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

David Chelimsky dchelimsky at gmail.com
Mon Feb 4 16:07:05 EST 2008

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

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.


> 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

More information about the rspec-users mailing list