7

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.

PlankTon
  • 12,443
  • 16
  • 84
  • 153

1 Answers1

12

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

put :update, :id => "14", :post_id=> "1", :comment => {'these' => 'params'}
monocle
  • 5,866
  • 2
  • 26
  • 22