ActionMailer测试ArgumentError:错误的参数数量(0表示1)

时间:2015-09-12 17:33:12

标签: ruby-on-rails rake actionmailer

无法弄清楚bundle exec rake test命令失败的原因,但是当我手动尝试发送电子邮件时,它会正常工作并按原样显示。

错误讯息: ArgumentError:参数数量错误(0表示1)并突出显示 来自def stock_alert_email(stock)

user_mailer.rb

user_mailer.rb

def stock_alert_email(stock)
    @stock = stock
    mail(to: 'email@email.org', subject: "Stock Alert! %s has reached %s" % [@stock[0].to_s, @stock[1].to_s])
end

user_mailer_test.rb

test "stock_alert_email" do
  email = UserMailer.stock_alert_email().deliver_now
  assert_not ActionMailer::Base.deliveries.empty?

  # Test the body of the sent email contains what we expect it to
  assert_equal ['email@email.org'], email.from
  assert_equal ['email@email.org'], email.to
  assert_equal "Stock Alert! %s has reached %s" % [@stock[0].to_s, @stock[1].to_s], email.subject
  assert_equal read_fixture('stock_alert_email').join, email.body.to_s
end

where I call the mail to send

UserMailer.stock_alert_email(@stock).deliver_now

当我在上面的^^行之前使用binding.pry时,我看到@stock是

[1] pry(main)> @stock
=> ["AAPL", 114.21]

1 个答案:

答案 0 :(得分:2)

问题出在这一行

email = UserMailer.stock_alert_email().deliver_now

在期望参数时,您没有将任何参数传递给stock_alert_email