Rspec 新手:嵌套控制器测试的快速示例?
我刚刚开始使用 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 下的“评论”模型进行类似的测试,但没有任何乐趣。任何建议表示赞赏。
I'm just starting out with RSpec and having a little difficulty writing up controller tests for nested resources. I've tried googling this, but without much luck.
Could someone offer a basic example of a "PUT update" test test ensures a nested resource is updated? Just to elaborate, I have the equivalent (non-nested) resource tested like this:
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
I've been trying for some time to correctly stub a similar test for a 'Comment' model which is nested under Post, but no joy. Any suggestions appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将两个 id 传递给您的 put 方法
You'll need to have both id's passed to your put method