邮件规范测试失败,但应该通过

时间:2015-02-23 17:27:30

标签: ruby-on-rails rspec actionmailer rspec-rails

在rails 4应用程序中,我有一个名为admin_mailer的邮件程序,它会在创建订单时发送电子邮件。

这是邮件:

  def order_notification(order)
    @order = order
    mail(
          :to => ENV['ADMIN_EMAIL'],
          :subject => "New order #{order.id}",
          :from => ENV['EMAIL_PROVIDER_USERNAME']
          )
  end

以下是相关观点:

h1
  New order!
%p
  Thank you
  = @order.user.name
  A new order has been created.

邮件发送正确,但仍然是以下测试失败:

scenario 'order_notification should show user name' do
    user = create(:user, name: 'Alan Ford')
    mycart = create(:cart)
    vest = create(:product, name: 'vest', price: '250000')
    jacket = create(:product, name: 'jacket-1', price: '300000')
    line_vest = create(:line_item, product: vest, unit_price: vest.price, cart: mycart)
    line_jacket = create(:line_item, product: jacket, unit_price: jacket.price, cart: mycart)
    order = create(:order, cart: mycart, user: user, amount: '550000')
    AdminMailer.order_notification(order).deliver_now
    puts ActionMailer::Base.deliveries.first.body
    expect(ActionMailer::Base.deliveries.first.body).to include 'Alan Ford'
  end

我在测试中添加了puts行,所以我可以在屏幕上看到测试的实际结果,这很好:

<h1>
New order!
</h1>
<p>
Thank you
Alan Ford
A new order has been created.
</p>

测试仍未通过:

  1) AdminMailer order_notification should send an email
     Failure/Error: AdminMailer.order_notification(order).deliver_now
     ActionView::Template::Error:
       undefined method `name' for nil:NilClass
     # ./app/views/admin_mailer/order_notification.html.haml:5:in `_app_views_admin_mailer_order_notification_html_haml___3279210695024268720_62832180'
     # /home/andrea/.rvm/gems/ruby-2.1.3/gems/actionview-4.2.0/lib/action_view/template.rb:145:in `block in render'
     # /home/andrea/.rvm/gems/ruby-2.1.3/gems/activesupport-4.2.0/lib/active_support/notifications.rb:166:in `instrument'
     # /home/andrea/.rvm/gems/ruby-2.1.3/gems/actionview-4.2.0/lib/action_view/template.rb:333:in `instrument'
     # /home/andrea/.rvm/gems/ruby-2.1.3/gems/actionview-4.2.0/lib/action_view/template.rb:143:in `render'
[... a lot more lines ...]
     # /home/andrea/.rvm/gems/ruby-2.1.3/gems/actionmailer-4.2.0/lib/action_mailer/message_delivery.rb:25:in `new'
     # /home/andrea/.rvm/gems/ruby-2.1.3/gems/actionmailer-4.2.0/lib/action_mailer/message_delivery.rb:25:in `__getobj__'
     # /home/andrea/.rvm/gems/ruby-2.1.3/gems/actionmailer-4.2.0/lib/action_mailer/message_delivery.rb:34:in `message'
     # /home/andrea/.rvm/gems/ruby-2.1.3/gems/actionmailer-4.2.0/lib/action_mailer/message_delivery.rb:85:in `deliver_now'
     # ./spec/mailers/admin_mailer_spec.rb:61:in `block (2 levels) in <top (required)>'

@ order.user不能为零,因为@ order.user.name在电子邮件中正确显示,我无法弄清楚出了什么问题。

0 个答案:

没有答案