Rspec 新手:嵌套控制器测试的快速示例?

发布于 2024-10-11 12:58:19 字数 677 浏览 4 评论 0原文

我刚刚开始使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠北森屋 2024-10-18 12:58:19

您需要将两个 id 传递给您的 put 方法

put :update, :id => "14", :post_id=> "1", :comment => {'these' => 'params'}

You'll need to have both id's passed to your put method

put :update, :id => "14", :post_id=> "1", :comment => {'these' => 'params'}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文