我有一個相當奇怪的rspec失敗,感覺像rspec或rspec-rails(rspec 3.4.0和rspec-rails 3.4中的測試配置問題或錯誤。 2)。我希望對此事有任何建議:Rspec測試郵件從控制器與輔助參數
我有一個控制器測試,它只是調用一個郵件程序的方法,其模板利用該助手,並從輔助函數中獲取錯誤,我使用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'
我試過設置助手和郵件的預期如果郵件的方法或輔助方法被調用返回零,但他們從來沒有解決這個錯誤。我對混淆郵件或幫手方法什麼也沒有感到困惑和驚訝。
下面是我試過的存根:
# 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的地方?
也許我錯過了一些東西,但是你不應該在你的郵件視圖中訪問params。 – Leito
參數用在助手中,而不是視圖中。郵件的視圖調用助手。我不知道那是有問題的。 – yekta
這是,你不能那樣做。如果你的助手使用params,你只能在控制器或視圖中使用它。 – Leito