Anthony Carlos
2007-Jun-15 00:48 UTC
[rspec-users] problem getting started with stubbing in rails controller specs
Hello, Ingo! I''m copying your question to a new e-mail instead of replying to try to keep message threads separate. Have you tried setting up your expectation before issuing the get statement? For example, take this out of the setup block: get ''index'', :locale => ''en'' and move it after: Station.should_receive(:find).with(:all).and_return(@mock_stations) I set-up inter-object expectations before exercising the controller action. Does this help? -Anthony PS: If you could be more specific about which concepts about mocks and stubs are confusing you, I''d be happy to try and help.l ---------- Hi, I am getting started with writing controller specs with rspec/rails, and seem to have trouble fully grasping the concept of mocks and stubs. I can''t figure out what''s wrong with the following. This is the action I want to spec: def index @stations = Station.find(:all, :order => ''name'') end and this is my spec: describe "GET to index" do controller_name :stations before(:each) do @mock_station = mock_model(Station) @mock_stations = [@mock_station] Station.stub!(:find).and_return(@mock_stations) get ''index'', :locale => ''en'' end it "should find stations" do Station.should_receive(:find).with(:all).and_return (@mock_stations) end end It fails with this error: Spec::Mocks::MockExpectationError in ''GET to index should find stations'' Station expected :find with (:all) once, but received it 0 times ./spec/controllers/stations_controller_spec.rb:22: I can see in the log that the database is not hit for Station.find (:all), so the stubbing seems to have worked. Guess I''m stuck. Thanks for any hint to get me going again! Ingo _______________________________________________ rspec-users mailing list rspec-users at rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Ingo Weiss
2007-Jun-15 17:22 UTC
[rspec-users] problem getting started with stubbing in rails controller specs
Ah, I see how it makes sense to do it in this order. However, the error does not go away when I state the expectation before issuing the request. I will keep investigating... Ingo On Jun 14, 2007, at 8:48 PM, Anthony Carlos wrote:> Hello, Ingo! > > I''m copying your question to a new e-mail instead of replying to try > to keep message threads separate. > > Have you tried setting up your expectation before issuing the get > statement? For example, take this out of the setup block: > get ''index'', :locale => ''en'' > > and move it after: > Station.should_receive(:find).with(:all).and_return(@mock_stations) > > I set-up inter-object expectations before exercising the controller > action. > > Does this help? > > -Anthony > > PS: If you could be more specific about which concepts about mocks > and stubs are confusing you, I''d be happy to try and help.l > > ---------- > > Hi, > > I am getting started with writing controller specs with rspec/rails, > and seem to have trouble fully grasping the concept of mocks and > stubs. I can''t figure out what''s wrong with the following. This is > the action I want to spec: > > def index > @stations = Station.find(:all, :order => ''name'') > end > > and this is my spec: > > > describe "GET to index" do > controller_name :stations > > before(:each) do > @mock_station = mock_model(Station) > @mock_stations = [@mock_station] > Station.stub!(:find).and_return(@mock_stations) > get ''index'', :locale => ''en'' > end > > it "should find stations" do > Station.should_receive(:find).with(:all).and_return > (@mock_stations) > end > > end > > > It fails with this error: > > Spec::Mocks::MockExpectationError in ''GET to index should find > stations'' > Station expected :find with (:all) once, but received it 0 times > ./spec/controllers/stations_controller_spec.rb:22: > > I can see in the log that the database is not hit for Station.find > (:all), so the stubbing seems to have worked. Guess I''m stuck. Thanks > for any hint to get me going again! > > Ingo > > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
Anthony Carlos
2007-Jun-15 18:46 UTC
[rspec-users] problem getting started with stubbing in rails controller specs
Ingo: I cannot find any documentation on the :all parameter to the "with" clause. Can you try it without the "with" clause? Station.should_receive(:find).and_return(@mock_stations) See if it goes through and let me know what happens. -Anthony On Jun 15, 2007, at 1:22 PM, Ingo Weiss wrote:> Ah, I see how it makes sense to do it in this order. However, the > error does not go away when I state the expectation before issuing > the request. I will keep investigating... > > Ingo > > > On Jun 14, 2007, at 8:48 PM, Anthony Carlos wrote: > >> Hello, Ingo! >> >> I''m copying your question to a new e-mail instead of replying to try >> to keep message threads separate. >> >> Have you tried setting up your expectation before issuing the get >> statement? For example, take this out of the setup block: >> get ''index'', :locale => ''en'' >> >> and move it after: >> Station.should_receive(:find).with(:all).and_return(@mock_stations) >> >> I set-up inter-object expectations before exercising the controller >> action. >> >> Does this help? >> >> -Anthony >> >> PS: If you could be more specific about which concepts about mocks >> and stubs are confusing you, I''d be happy to try and help.l >> >> ---------- >> >> Hi, >> >> I am getting started with writing controller specs with rspec/rails, >> and seem to have trouble fully grasping the concept of mocks and >> stubs. I can''t figure out what''s wrong with the following. This is >> the action I want to spec: >> >> def index >> @stations = Station.find(:all, :order => ''name'') >> end >> >> and this is my spec: >> >> >> describe "GET to index" do >> controller_name :stations >> >> before(:each) do >> @mock_station = mock_model(Station) >> @mock_stations = [@mock_station] >> Station.stub!(:find).and_return(@mock_stations) >> get ''index'', :locale => ''en'' >> end >> >> it "should find stations" do >> Station.should_receive(:find).with(:all).and_return >> (@mock_stations) >> end >> >> end >> >> >> It fails with this error: >> >> Spec::Mocks::MockExpectationError in ''GET to index should find >> stations'' >> Station expected :find with (:all) once, but received it 0 times >> ./spec/controllers/stations_controller_spec.rb:22: >> >> I can see in the log that the database is not hit for Station.find >> (:all), so the stubbing seems to have worked. Guess I''m stuck. Thanks >> for any hint to get me going again! >> >> Ingo >> >> >> >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
Ingo Weiss
2007-Jun-15 18:53 UTC
[rspec-users] problem getting started with stubbing in rails controller specs
Thanks so much, Anthony, this works! I thought that .with(:all) would set the expectation that ''find'' is called with the :all parameter like this: Station.find(:all) but apparently that is not what''s happening Ingo On Jun 15, 2007, at 2:46 PM, Anthony Carlos wrote:> Ingo: > > I cannot find any documentation on the :all parameter to the "with" > clause. Can you try it without the "with" clause? > > Station.should_receive(:find).and_return(@mock_stations) > > See if it goes through and let me know what happens. > > -Anthony > > > On Jun 15, 2007, at 1:22 PM, Ingo Weiss wrote: > >> Ah, I see how it makes sense to do it in this order. However, the >> error does not go away when I state the expectation before issuing >> the request. I will keep investigating... >> >> Ingo >> >> >> On Jun 14, 2007, at 8:48 PM, Anthony Carlos wrote: >> >>> Hello, Ingo! >>> >>> I''m copying your question to a new e-mail instead of replying to try >>> to keep message threads separate. >>> >>> Have you tried setting up your expectation before issuing the get >>> statement? For example, take this out of the setup block: >>> get ''index'', :locale => ''en'' >>> >>> and move it after: >>> Station.should_receive(:find).with(:all).and_return(@mock_stations) >>> >>> I set-up inter-object expectations before exercising the controller >>> action. >>> >>> Does this help? >>> >>> -Anthony >>> >>> PS: If you could be more specific about which concepts about mocks >>> and stubs are confusing you, I''d be happy to try and help.l >>> >>> ---------- >>> >>> Hi, >>> >>> I am getting started with writing controller specs with rspec/rails, >>> and seem to have trouble fully grasping the concept of mocks and >>> stubs. I can''t figure out what''s wrong with the following. This is >>> the action I want to spec: >>> >>> def index >>> @stations = Station.find(:all, :order => ''name'') >>> end >>> >>> and this is my spec: >>> >>> >>> describe "GET to index" do >>> controller_name :stations >>> >>> before(:each) do >>> @mock_station = mock_model(Station) >>> @mock_stations = [@mock_station] >>> Station.stub!(:find).and_return(@mock_stations) >>> get ''index'', :locale => ''en'' >>> end >>> >>> it "should find stations" do >>> Station.should_receive(:find).with(:all).and_return >>> (@mock_stations) >>> end >>> >>> end >>> >>> >>> It fails with this error: >>> >>> Spec::Mocks::MockExpectationError in ''GET to index should find >>> stations'' >>> Station expected :find with (:all) once, but received it 0 times >>> ./spec/controllers/stations_controller_spec.rb:22: >>> >>> I can see in the log that the database is not hit for Station.find >>> (:all), so the stubbing seems to have worked. Guess I''m stuck. >>> Thanks >>> for any hint to get me going again! >>> >>> Ingo >>> >>> >>> >>> >>> _______________________________________________ >>> rspec-users mailing list >>> rspec-users at rubyforge.org >>> http://rubyforge.org/mailman/listinfo/rspec-users >>> >>> _______________________________________________ >>> rspec-users mailing list >>> rspec-users at rubyforge.org >>> http://rubyforge.org/mailman/listinfo/rspec-users >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
Kyle Hargraves
2007-Jun-15 20:16 UTC
[rspec-users] problem getting started with stubbing in rails controller specs
On Jun 15, 2007, at 1:53 PM, Ingo Weiss wrote:> Thanks so much, Anthony, this works! > > I thought that .with(:all) would set the expectation that ''find'' is > called with the :all parameter like this: > > Station.find(:all) > > but apparently that is not what''s happening > > IngoIt is indeed expecting ''find'' to be called with(:all), but you aren''t doing so: @stations = Station.find(:all, :order => ''name'') All of the parameters are taken into account. IIRC you can''t use with (:all, any_args) either, though I might be wrong. Kyle
Tom Stuart
2007-Jun-15 20:21 UTC
[rspec-users] problem getting started with stubbing in rails controller specs
On 15 Jun 2007, at 19:53, Ingo Weiss wrote:> I thought that .with(:all) would set the expectation that ''find'' is > called with the :all parameter like this: > > Station.find(:all) > > but apparently that is not what''s happeningThat is what .with(:all) does, but in your controller you''re doing @stations = Station.find(:all, :order => ''name'') i.e. .with(:all, :order => ''name''), so it''s no surprise the spec fails! :-) Cheers, -Tom
Patrick Lenz
2007-Jun-15 20:43 UTC
[rspec-users] problem getting started with stubbing in rails controller specs
On Jun 15, 2007, at 10:16 PM, Kyle Hargraves wrote:> It is indeed expecting ''find'' to be called with(:all), but you aren''t > doing so: > > @stations = Station.find(:all, :order => ''name'') > > All of the parameters are taken into account. IIRC you can''t use with > (:all, any_args) either, though I might be wrong.You should actually be able to invoke it this way: Station.should_receive(:find).with(:all, anything).and_return (@mock_stations) Best, Patrick