Mocha expectation is affecting (bleeding) from one test to another

Mocha expectation is affecting (bleeding) from one test to another:

In one test, I have:
Friendship.expects(:is_friend?).with(users[1], users[3]).returns(true)
Then, in another test, which actually is listed before the test, I am
getting this error:

unexpected invocation: Friendship(id: integer, user_id: integer,
friend_id: integer, status: string, requested_at: datetime,
accepted_at: datetime, created_at: datetime, updated_at:
datetime).is_friend?(#User:0xb6b2bf04, #User:0xb6bc7648)
satisfied expectations:

  • expected exactly once, already invoked once: Friendship(id: integer,
    user_id: integer, friend_id: integer, status: string, requested_at:
    datetime, accepted_at: datetime, created_at: datetime, updated_at:
    datetime).is_friend?(#User:0xb6eeb220, #User:0xb6c3a224)

What’s going on? I am not using test-unit gem, or shoulda. I removed
'require ‘mocha’ from the top of the test file but the problem remains
the same. Interestingly, it doesn’t happen on my Mac machine. It
only happens on my staging server which is Ubuntu. I am on rails
2.3.5.

Thanks.

I realise this probably won’t help you directly because you state that
you’re not using shoulda but I had a similar problem with mocha stubs
bleeding from one Shoulda test to another.

I found that reversing the order of the config.gem lines in
environment.rb, thus:

config.gem “thoughtbot-shoulda”, :lib => “shoulda” #- shoulda must
be loaded before mocha
config.gem “mocha” #–or
any_instance bleeds between tests

resolved the problem. I didn’t investigate why.

Also, this problem didn’t seem to occur when I ran under autotest. It
only popped up when running the tests with rake. I expect because
autotest runs the tests as separate processes.

Good luck
Ritchie

Are you sure no expectations are being set for the failing test e.g.
in a setup method?

What version of Ruby are you using? What version of Mocha are you
using. How and where are you requiring Mocha?

Can you send the test code and the code under test to the Mocha
mailing list [1] so we can try and reproduce the problem.

Thanks, James.

[1] http://groups.google.com/group/mocha-developer