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