首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Rspec3:向控制器发送异常

Rspec3:向控制器发送异常
EN

Stack Overflow用户
提问于 2014-07-10 12:48:44
回答 1查看 73关注 0票数 0

我想测试我的控制器,并确保它在接收到ActiveRecord::RecordNotFound时呈现适当的模板。

控制员:

代码语言:javascript
运行
复制
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

我的测试:

代码语言:javascript
运行
复制
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>

如何正确地写我的考试?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-13 09:08:21

我成功地用以下代码模拟了一个ActiveRecord::RecordNotFound

代码语言:javascript
运行
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24677007

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档