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.
You'll need to have both id's passed to your put method
put :update, :id => "14", :post_id=> "1", :comment => {'these' => 'params'}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With