Struggling mightily! Testing a controller called Govtpositions. Just focusing on testing the create method for the moment. I would imagine there should be two examples for a successful save... instantiate the model object and then save it, is that right? So just to test the model object instantiation, I have: describe GovtpositionsController, do before do Govtposition.stub!(:new).and_return(@govtposition) end it "should create a new govt position model" do @govtposition = mock_model(Govtposition) @params = {"name"=>"chief"} Govtposition.should_receive(:new).with(@params).and_return (@govtposition) end end This fails, with the reason that @govtposition is receiving an unexpected :save. Well I didn''t intend to test save, I was planning to write another example for that assertion. Why can''t I just test object creation in it''s own example, must I also test saving in the same example? I''m clearly missing something basic! thanks in advance for any insight you can offer
Norm, I don''t test instantiation and then saving, I test successful and unsuccessful creation. By testing this concern, I don''t need to worry about expectations or mocks, I actually create the object (or not!) and then test that the controller behaves the way I expect it to in these situations. Its probably not important that Govtpostion receives "new". It''s important that when it receives new with the proper things, and then saves, that it behaves a certain way. And when it doesn''t receive the proper things, it acts another way. Hope that helps, BJ Clark On Jul 24, 2009, at 5:13 PM, norm wrote:> Struggling mightily! > > Testing a controller called Govtpositions. Just focusing on testing > the create method for the moment. I would imagine there should be two > examples for a successful save... instantiate the model object and > then save it, is that right? > > So just to test the model object instantiation, I have: > > describe GovtpositionsController, do > before do > Govtposition.stub!(:new).and_return(@govtposition) > end > > it "should create a new govt position model" do > @govtposition = mock_model(Govtposition) > @params = {"name"=>"chief"} > Govtposition.should_receive(:new).with(@params).and_return > (@govtposition) > end > > end > > This fails, with the reason that @govtposition is receiving an > unexpected :save. Well I didn''t intend to test save, I was planning to > write another example for that assertion. Why can''t I just test object > creation in it''s own example, must I also test saving in the same > example? I''m clearly missing something basic! > > thanks in advance for any insight you can offer > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users----- BJ Clark AboutUs.Org
On Fri, Jul 24, 2009 at 7:13 PM, norm<codehacker at comcast.net> wrote:> Struggling mightily! > > Testing a controller called Govtpositions. Just focusing on testing > the create method for the moment. I would imagine there should be two > examples for a successful save... instantiate the model object and > then save it, is that right? > > So just to test the model object instantiation, I have: > > describe GovtpositionsController, do > ?before do > ? ?Govtposition.stub!(:new).and_return(@govtposition) > ?end > > ?it "should create a new govt position model" do > ? ?@govtposition = mock_model(Govtposition) > ? ?@params = {"name"=>"chief"} > ? ?Govtposition.should_receive(:new).with(@params).and_return > (@govtposition) > ?end > > end > > This fails, with the reason that @govtposition is receiving an > unexpected :save. Well I didn''t intend to test save,Sure, but if the code does that, then you have to account for it. You can just stub save and not expect it. That''s a perfect case for a method stub - something unimportant to your example, but necessary for the example to run.> I was planning to > write another example for that assertion. Why can''t I just test object > creation in it''s own example, must I also test saving in the same > example? I''m clearly missing something basic! > > thanks in advance for any insight you can offerAssuming you''re dealing w/ a standard create action, IMO, you don''t really need to spec the instantiation of the object. This is one of those gray areas in which you''re touching implementation with stubs and mocks, but you''re not really specifying implementation. i.e., the examples might read: GovtpositionsController POST create with valid data creates a new govtposition with the submitted data redirects to the list of govtpositions If you''re using mocks/stubs for isolation and speed, you may need to stub :new if that''s what the create method is using, but that''s just there to get the example to execute - not because it''s part of what''s being specified. Make sense? Cheers, David