测试控制器 - 使用包含查找时未找到记录
我正在测试在 find 语句中使用 include 的控制器操作。测试运行时它会引发 RecordNotFound。我错过了什么吗?我应该如何处理这些事情的测试?
控制器:
def show
@forum_sub_topic = ForumSubTopic.includes(:forum_posts => [:post_replies]).find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @forum_sub_topic }
end
end
测试:
it 'renders show template' do
ForumSubTopic.stub(:find).with("37") { mock_forum_sub_topic }
get :show, :id => "37"
response.should render_template('show')
ebd
I'm testing a controller action that uses includes in the find statement. It raises RecordNotFound when the test runs. Am I missing something? How should I be handling tests on such things?
Controller:
def show
@forum_sub_topic = ForumSubTopic.includes(:forum_posts => [:post_replies]).find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @forum_sub_topic }
end
end
Test:
it 'renders show template' do
ForumSubTopic.stub(:find).with("37") { mock_forum_sub_topic }
get :show, :id => "37"
response.should render_template('show')
ebd
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在
ForumSubTopic
上存根:find
,但您的控制器正在ActiveRecord::Relation
上调用.find
对象而不是ForumSubTopic
模型。如果你不太关心
with("37")
部分(因为我不确定是否可以这样做),RSpec 提供了一个stub_chain
方法这应该适合你:否则,你可以将多个存根放入:
You're stubbing
:find
onForumSubTopic
, but your controller is calling.find
on anActiveRecord::Relation
object rather than theForumSubTopic
model.If you don't care so much about the
with("37")
part (because I'm not sure if it's possible doing this), RSpec provides astub_chain
method that should work for you:Otherwise, you could put multiple stubs in: