简体   繁体   English

使用 Mailer 进行 rspec 测试失败

[英]rspec failing tests with Mailer

I am getting an issue where the tests will not pass as it does not recognise user on completed_payments which is a method within the listing model.我遇到了一个问题,即测试无法通过,因为它无法识别completed_payments上的user ,这是列表模型中的一种方法。 Would anyone know why this is happening?有谁知道为什么会这样?

Error is:错误是:

1) ListingMailer#sold_email Failure/Error: 1) ListingMailer#sold_email 失败/错误:

Your item was bought by <%= @listing.completed_payment.user.username %> (<%= @listing.completed_payment.user.paypal_email %>) for the listed price of £<%= "%.2f" % (@listing.price_cents/100.00) %> .您的商品由<%= @listing.completed_pa​​yment.user.username %> (<%= @listing.completed_pa​​yment.user.paypal_email %>)£<%= "%.2f" % (@ list.price_cents/100.00) %>

ActionView::Template::Error: undefined method `user' for nil:NilClass ActionView::Template::Error: 未定义方法 `user' 为 nil:NilClass

Files as follows:文件如下:

listing_mailer.rb list_mailer.rb

                        <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>

sold_email.html.erb sold_email.html.erb

  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

listing_mailer_spec.rb 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

Thank you谢谢

I believe this is happening because you haven't called payment to instantiate it.我相信这是因为你没有调用payment来实例化它。 Therefore, there is no payment on the listings object.因此,listings 对象没有付款。

There are a number of ways around this, you can just call payment before your expectations or do something like有很多方法可以解决这个问题,您可以在您的期望之前致电payment或执行类似的操作

ListingMailer.sold_email(payment.listing)

or, have a trait in your listings factory so that it will create a complete payment object for you.或者,在您的列表工厂中有一个特征,以便它为您创建一个完整的付款对象。 Something like create(:listing, :active, :complete_payment)类似于create(:listing, :active, :complete_payment)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM