[rspec-users] Mock objects not getting unloaded between specs

Nate Clark nate at nateclark.com
Mon Feb 4 15:51:50 EST 2008

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 =>

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

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

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?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-users/attachments/20080204/a766a098/attachment.html 

More information about the rspec-users mailing list