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

**URL:** <https://www.ruby-forum.com/t/mocha-expectation-is-affecting-bleeding-from-one-test-to-another/184712>\
**Category:** Rails\
**Created:** [March 7, 2010, 7:15am UTC](https://www.ruby-forum.com/t/mocha-expectation-is-affecting-bleeding-from-one-test-to-another/184712 "2010-03-07T07:15:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [March 7, 2010, 7:15am UTC](https://www.ruby-forum.com/t/mocha-expectation-is-affecting-bleeding-from-one-test-to-another/184712/1 "2010-03-07T07:15:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [March 7, 2010, 1:35pm UTC](https://www.ruby-forum.com/t/mocha-expectation-is-affecting-bleeding-from-one-test-to-another/184712/2 "2010-03-07T13:35:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [March 7, 2010, 6:34pm UTC](https://www.ruby-forum.com/t/mocha-expectation-is-affecting-bleeding-from-one-test-to-another/184712/3 "2010-03-07T18:34:03Z")

</div>

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](http://groups.google.com/group/mocha-developer)
