Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec newbie: Quick example of nested controller test?

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.

like image 654
PlankTon Avatar asked Jan 13 '11 15:01

PlankTon


1 Answers

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

put :update, :id => "14", :post_id=> "1", :comment => {'these' => 'params'}
like image 164
monocle Avatar answered Oct 03 '22 00:10

monocle