0

我想测试我的控制器并确保它在收到ActiveRecord::RecordNotFound

控制器:

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>

如何正确编写我的测试?

4

1 回答 1

0

我设法ActiveRecord::RecordNotFound用以下代码模拟了一个:

it "renders questionnaire/verses_not_found.html if theres an ActiveRecord::RecordNotFound exception" do
  verse_selector = double("verse_selector")
  allow(verse_selector).to receive(:select_verses).and_return(ActiveRecord::RecordNotFound.new())
  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
于 2014-07-13T09:08:21.603 回答