rspec 使用 Mailer 的测试失败
rspec failing tests with Mailer
我遇到了一个问题,测试不会通过,因为它无法识别 completed_payments
上的 user
,这是列表模型中的一种方法。有人知道为什么会这样吗?
错误是:
1) ListingMailer#sold_email
Failure/Error:
您的商品由 <%= @listing.completed_payment.user.username %> (<%= @listing.completed_payment.user.paypal_email %>) 购买上市价格 £<%= "%.2f" % (@listing.price_cents/100.00) %>.
ActionView::Template::Error:
nil:NilClass 的未定义方法“用户”
文件如下:
listing_mailer.rb
class ListingMailer < ApplicationMailer
def sold_email(listing)
@user = listing.user
@listing = listing
mail(to: listing.user_email, subject: 'You have sold one of your listings')
end
sold_email.html.erb
<p>Dear <%= @user.first_name %>,</p>
<p>Good news! You have made a sale:</p>
<!-- Action -->
<p> <%= link_to "#{@listing.version_title} by #{@listing.artist_name}", listing_url(@listing) %></p>
<p> Your item was bought by <strong><%= @listing.completed_payment.user.username %> (<%= @listing.completed_payment.user.paypal_email %>)</strong> for the listed price of <strong>£<%= "%.2f" % (@listing.price_cents/100.00) %></strong>.</p>
<p>The Paypal reference is <%= @listing.completed_payment.paypal_reference %>.</p>
<p>Thanks for using elvinyl,</p>
<p>Keith and the elvinyl team</p>
listing_mailer_spec.rb
describe '#sold_email' do
let(:listing) { create(:listing, :active) }
let(:payment) { create(:payment, status: :finished, listing: listing) }
let(:email) { ListingMailer.sold_email(listing) }
it { expect(email.to).to eq([listing.user_email]) }
it { expect(email.from).to eq(ENV.fetch('EMAIL_FROM_ADDRESS')) }
it { expect(email.subject).to eq 'You have sold one of your listings' }
end
谢谢
我相信这是因为您没有调用 payment
来实例化它。因此,listings 对象上没有付款。
有很多方法可以解决这个问题,您可以在预期之前调用 payment
或执行类似
的操作
ListingMailer.sold_email(payment.listing)
或者,在您的列表工厂中有一个特征,以便它会为您创建一个完整的支付对象。类似于 create(:listing, :active, :complete_payment)
我遇到了一个问题,测试不会通过,因为它无法识别 completed_payments
上的 user
,这是列表模型中的一种方法。有人知道为什么会这样吗?
错误是:
1) ListingMailer#sold_email Failure/Error:
您的商品由 <%= @listing.completed_payment.user.username %> (<%= @listing.completed_payment.user.paypal_email %>) 购买上市价格 £<%= "%.2f" % (@listing.price_cents/100.00) %>.
ActionView::Template::Error: nil:NilClass 的未定义方法“用户”文件如下:
listing_mailer.rb
class ListingMailer < ApplicationMailer
def sold_email(listing)
@user = listing.user
@listing = listing
mail(to: listing.user_email, subject: 'You have sold one of your listings')
end
sold_email.html.erb
<p>Dear <%= @user.first_name %>,</p>
<p>Good news! You have made a sale:</p>
<!-- Action -->
<p> <%= link_to "#{@listing.version_title} by #{@listing.artist_name}", listing_url(@listing) %></p>
<p> Your item was bought by <strong><%= @listing.completed_payment.user.username %> (<%= @listing.completed_payment.user.paypal_email %>)</strong> for the listed price of <strong>£<%= "%.2f" % (@listing.price_cents/100.00) %></strong>.</p>
<p>The Paypal reference is <%= @listing.completed_payment.paypal_reference %>.</p>
<p>Thanks for using elvinyl,</p>
<p>Keith and the elvinyl team</p>
listing_mailer_spec.rb
describe '#sold_email' do
let(:listing) { create(:listing, :active) }
let(:payment) { create(:payment, status: :finished, listing: listing) }
let(:email) { ListingMailer.sold_email(listing) }
it { expect(email.to).to eq([listing.user_email]) }
it { expect(email.from).to eq(ENV.fetch('EMAIL_FROM_ADDRESS')) }
it { expect(email.subject).to eq 'You have sold one of your listings' }
end
谢谢
我相信这是因为您没有调用 payment
来实例化它。因此,listings 对象上没有付款。
有很多方法可以解决这个问题,您可以在预期之前调用 payment
或执行类似
ListingMailer.sold_email(payment.listing)
或者,在您的列表工厂中有一个特征,以便它会为您创建一个完整的支付对象。类似于 create(:listing, :active, :complete_payment)