Nate Clark
2008-Feb-04 20:51 UTC
[rspec-users] Mock objects not getting unloaded between specs
I''m having a frustrating experience with a mock object in one example (a Rails controller test) interfering with an actual model test in a subsequent model example. More specifically, I have spec/controllers/section_controller_spec.rb with a mock call like this in one of the examples: Section.should_receive(:paginate_by_tag_set).with([], :page => nil).and_return([]) Ok, everything is fine so far. The mock does what it is supposed to and my example succeeds. Later on, I have a model spec in spec/models/section_spec.rb that actually tests the functionality of this "paginate_by_tag_set" class method, like this: Section.paginate_by_tag_set([], :page => nil).total_entries.should == 19 This model example fails, and I''ve determined that the code for my paginate_by_tag_set class method is never even getting called in this example. It seems to have something to do with the order in which my tests are run ... whenever I run the controller test before the model tests, this model example fails, presumably because the mocked call to paginate_by_tag_set is still living, and the actual code isn''t getting called. If I run spec spec/models/section_spec.rb individually, everything passes just fine. Any ideas on how to isolate the mock to live only within the test in which it is defined? Am I understanding something wrong here? Thanks, Nate -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20080204/a766a098/attachment.html
David Chelimsky
2008-Feb-04 21:07 UTC
[rspec-users] Mock objects not getting unloaded between specs
On Feb 4, 2008 2:51 PM, Nate Clark <nate at nateclark.com> wrote:> I''m having a frustrating experience with a mock object in one example (a > Rails controller test) interfering with an actual model test in a subsequent > model example. More specifically, I have > spec/controllers/section_controller_spec.rb with a mock call like this in > one of the examples: > > Section.should_receive(:paginate_by_tag_set).with([], :page => > nil).and_return([]) > > Ok, everything is fine so far. The mock does what it is supposed to and my > example succeeds. > Later on, I have a model spec in spec/models/section_spec.rb that actually > tests the functionality of this "paginate_by_tag_set" class method, like > this: > > Section.paginate_by_tag_set([], :page => nil).total_entries.should == 19 > > This model example fails, and I''ve determined that the code for my > paginate_by_tag_set class method is never even getting called in this > example. It seems to have something to do with the order in which my tests > are run ... whenever I run the controller test before the model tests, this > model example fails, presumably because the mocked call to > paginate_by_tag_set is still living, and the actual code isn''t getting > called.This was a bug a long time ago (probably a year), so unless you''re on a very old version I doubt it''s related. What version of rspec are you using? Also - can you pastie the spec and code for this controller? It would help to see what else is going on that might be leading to this. Thanks, David> > If I run spec spec/models/section_spec.rb individually, everything passes > just fine. > > Any ideas on how to isolate the mock to live only within the test in which > it is defined? Am I understanding something wrong here? > > Thanks, > Nate > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Nate Clark
2008-Feb-05 00:54 UTC
[rspec-users] Mock objects not getting unloaded between specs
David, thanks for getting back. Here is the pastie with (hopefully) all the relevant code: http://pastie.caboo.se/147536 We''re using the latest and greatest Rspec 1.1.3, so I don''t think it''s old versions that is the problem. Through some debugging, I have determined that in fact my "paginate_by_tag_set" method is never getting called in the model spec example as long as the section_controller spec is run first. If I run the model spec alone, or before the controller spec where the mock is defined, it works fine. Nate On Feb 4, 2008 1:07 PM, David Chelimsky <dchelimsky at gmail.com> wrote:> On Feb 4, 2008 2:51 PM, Nate Clark <nate at nateclark.com> wrote: > > I''m having a frustrating experience with a mock object in one example (a > > Rails controller test) interfering with an actual model test in a > subsequent > > model example. More specifically, I have > > spec/controllers/section_controller_spec.rb with a mock call like this > in > > one of the examples: > > > > Section.should_receive(:paginate_by_tag_set).with([], :page => > > nil).and_return([]) > > > > Ok, everything is fine so far. The mock does what it is supposed to and > my > > example succeeds. > > Later on, I have a model spec in spec/models/section_spec.rb that > actually > > tests the functionality of this "paginate_by_tag_set" class method, like > > this: > > > > Section.paginate_by_tag_set([], :page => nil).total_entries.should == 19 > > > > This model example fails, and I''ve determined that the code for my > > paginate_by_tag_set class method is never even getting called in this > > example. It seems to have something to do with the order in which my > tests > > are run ... whenever I run the controller test before the model tests, > this > > model example fails, presumably because the mocked call to > > paginate_by_tag_set is still living, and the actual code isn''t getting > > called. > > This was a bug a long time ago (probably a year), so unless you''re on > a very old version I doubt it''s related. What version of rspec are you > using? > > Also - can you pastie the spec and code for this controller? It would > help to see what else is going on that might be leading to this. > > Thanks, > David > > > > > If I run spec spec/models/section_spec.rb individually, everything > passes > > just fine. > > > > Any ideas on how to isolate the mock to live only within the test in > which > > it is defined? Am I understanding something wrong here? > > > > Thanks, > > Nate > > > > _______________________________________________ > > 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 >-- http://nateclark.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20080204/d67ce3af/attachment-0001.html