Chris Olsen
2007-Oct-16 16:48 UTC
[rspec-users] Does a mock model have to satisfy contraints
I am a little confused to the what happens with the following: before do @site = mock_model(Site, :to_param => "1") Site.stub!(:new).and_return(@site) end def post_with_successful_save @site.should_receive(:save).and_return(true) post :create, :site => {} end I understand that the "@site.should_receive(:save)..." places a check to whether the method is called and that it should return "true", but will the actual method within the model be called, or does it also create a stub to allow for the separation with the model. The reason that I ask is that in the following line "post :create, ..." there are no params passed into the post, yet is passes even though there are many validates_presence_of constraints within the model. The reason for the confusion is that in a previous project I had failures on many of the auto-generated controller tests and the only way to make them pass was to ensure all the controller post and put tests used a model that satisfied all the constraints. On the current project all the tests are passing when using mock models with no attributes set. Thanks for the help -- Posted via http://www.ruby-forum.com/.
David Chelimsky
2007-Oct-16 17:00 UTC
[rspec-users] Does a mock model have to satisfy contraints
On 10/16/07, Chris Olsen <lists at ruby-forum.com> wrote:> I am a little confused to the what happens with the following: > > before do > @site = mock_model(Site, :to_param => "1") > Site.stub!(:new).and_return(@site) > end > > def post_with_successful_save > @site.should_receive(:save).and_return(true) > post :create, :site => {} > end > > I understand that the "@site.should_receive(:save)..." places a check to > whether the method is called and that it should return "true"This is a bit tricky. What you''re setting up here (which is true of message expectations in general, not must mock_model) is an expectation that @site will receive the message :save and that when it does receive it, you are instructing it to return true (not expecting that the real :save method returns true). The real :save method is not called. Make sense?
Chris Olsen
2007-Oct-16 17:08 UTC
[rspec-users] Does a mock model have to satisfy contraints
That is what I thought (and was hoping for) Thanks David.> This is a bit tricky. What you''re setting up here (which is true of > message expectations in general, not must mock_model) is an > expectation that @site will receive the message :save and that when it > does receive it, you are instructing it to return true (not expecting > that the real :save method returns true). > > The real :save method is not called. > > Make sense?-- Posted via http://www.ruby-forum.com/.