我刚刚开始使用RSpec,并且在编写嵌套资源的控制器测试时遇到一些困难.我试过谷歌搜索,但没有太多运气.
有人可以提供“PUT更新”测试测试的基本示例,以确保更新嵌套资源吗?只是详细说明,我有像这样测试的等效(非嵌套)资源:
- def mock_post(stubs={})
- @mock_post ||= mock_model(Post,stubs).as_null_object
- end
- ...
- describe "PUT update" do
- describe "with valid parameters" do
- it "updates the requested post" do
- Post.stub(:find).with("14") { mock_post }
- mock_post.should_receive(:update_attributes).with({'these' => 'params'})
- put :update,:id => "14",:post => {'these' => 'params'}
- end
- end
- end
我已经尝试了一段时间来正确地为“评论”模型存储类似的测试,该模型嵌套在Post下,但没有快乐.任何建议赞赏.
解决方法
您需要将两个id传递给put方法
- put :update,:post_id=> "1",:comment => {'these' => 'params'}