0
我想測試我的控制器,並確保每當收到一個ActiveRecord::RecordNotFound
Rspec3:發送異常控制器
控制器它呈現了正確的模板:
class QuestionnaireController < ApplicationController
rescue_from ActiveRecord::RecordNotFound, with: :verses_not_found
def poem
@questionnaire = JSON.parse(session[:questionnaire], symbolize_names: true)
rawVerses = VerseSelector.select_verses(@questionnaire[:trait_category], @questionnaire[:message_category])
@poem = PoemCustomizer.customize_poem(rawVerses, @questionnaire)
end
def verses_not_found
render 'questionnaire/verses_not_found'
end
end
我的測試:
describe "GET 'poem'" do
it "renders questionnaire/verses_not_found.html if theres an ActiveRecord::RecordNotFound exception" do
verse_selector = double("VerseSelector", select_verses: ActiveRecord::RecordNotFound.new("Verses not found"))
get 'poem', nil, {questionnaire: {receiver_name:"a",location:"b",relationship:"coach",trait_category:"adventurous venturous",message_category:"You hurt my feelings"}}.to_json
expect(response).to render_template(:verses_not_found)
end
end
我不確定我的測試,它目前會產生一個錯誤:NoMethodError: undefined method 'each' for #<String:0xbcdf388>
如何正確編寫我的測試?
我存根詩提高RecordNotFound,然後進行測試,看是否調用詩自己,當收到verses_not_found。然後再進行一次測試,看看後一種方法是否能夠提供正確的頁面 –
我會創建一個匿名動作,引發RecordNotFound並獲取該動作來驗證預期的模板是否呈現 – Benj