Hey all,
I’m new to Rspec and I was just wondering what the standard way of
testing nested resources is?
I currently have a Job model which has many Correspondences.
The new action of my CorrespondencesController looks like:
def new
@job = Job.find params[:job_id]
@correspondence = @job.correspondences.build
end
And my current spec looks like:
it “GET new” do
@job = stub_model(Job, :to_param => “1”)
Job.should_receive(:find).and_return(@job)
get :new, :job_id => @job.id
assigns[:correspondence].should_not be_nil
end
The test is currently failing with correspondence is nil, but it works
fine in the browser.
Any help here would be great, thanks.
Thanks for your response Pat - I ended up taking the stub_chain method
which got it working. For anyone interested this is my new test:
it "GET new" do
@correspondence = mock_model(Correspondence)
@job.stub_chain(:correspondences,
:build).and_return(@correspondence)
get :new, :job_id => @job
assigns[:correspondence].should be_an_instance_of Correspondence
end
Try assigns(:correspondence) instead. If that doesn’t work, I would
inspect the value of @job.correspondences.build (when run in the test).
It’s possible that stub_model is causing #build to return nil – I don’t
remember the behavior off the top of my head and don’t have a Ruby
available to check. Anyway if it is nil then you’ll need to do some more
stubbing setup. stub_chain may be useful.
Pat