来自控制器的Rspec测试邮件程序,辅助程序中的params

时间:2016-05-25 17:28:09

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

我有一个相当奇怪的rspec失败,感觉像测试配置问题或rspec或rspec-rails(rspec 3.4.0和rspec-rails 3.4.2)中的错误。我希望对此事有任何建议:

我有一个控制器测试,只是调用一个邮件程序方法,其模板使用该帮助程序,我从助手函数中得到一个错误,我使用了params。

describe MyController, :type => :controller do
  describe "POST send_a_specific_type_of_email" do
    it "sends the fact sheet" do
      ActionMailer::Base.deliveries = []
     post :send_a_specific_type_of_email, params
     expect(ActionMailer::Base.deliveries.count).to eq(1)
    end
  end
end

这会导致错误,这与我使用params的辅助方法有关。

Failure/Error: if params[:some_value] == "on"

ActionView::Template::Error:
 undefined method `params' for #<MyMailer:0x007fdcf56020f0>
# ./app/helpers/mycontroller_helper.rb:139:in `display_mycontroller_price'
# ./app/views/mycontroller/_items.html.erb:26:in `_app_views_mycontroller__items_html_erb__2241527061928464634_70293475039080'
# ./app/views/production_mailer/email_mycontroller.html.erb:17:in `_app_views_production_mailer_email_mycontroller_html_erb___301577404429349505_70293454886780'
# ./app/models/production_mailer.rb:51:in `email_mycontroller'
# ./app/controllers/mycontroller_controller.rb:40:in `send_a_specific_type_of_email'
# ./lib/authenticated_system.rb:75:in `catch_unauthorized'
# ./spec/controllers/mycontroller_controller_spec.rb:109:in `block (4 levels) in <top (required)>'
# ------------------
# --- Caused by: ---
# NoMethodError:
#   undefined method `params' for #<MyMailer:0x007fdcf56020f0>
#   ./app/helpers/mycontroller_helper.rb:139:in `display_mycontroller_price'

如果调用了邮件程序方法或帮助程序方法但我们从未解决该错误,我已尝试设置帮助程序和邮件程序返回nil的期望值。我很困惑也很惊讶于邮寄邮件或帮助程序方法没有做任何事情。

以下是我试图抄袭的方法:

# does nothing
expect(MyHelper).to receive(:display_mycontroller_price).and_return(nil)

# does cover up the issue but causes the above test to fail because 
# deliver_now quantities are no longer being checked, thus negating the purpose of the test in the first place
expect(FactSheetHelper).to receive(:display_factsheet_price).with(project.data).and_return(0)
expect(ProductionMailer).to receive(:params).and_return(params)
expect(message_delivery).to receive(:deliver_now)

如何成功地从控制器规范测试中存储Helper方法,或者这是某个rspec中的错误?

1 个答案:

答案 0 :(得分:1)

错误说明了一切,你不能在邮件程序中使用params(或通过帮助程序在邮件程序的上下文中)。重构,所以你将值作为参数传递给邮件程序方法。