# Using rspec and mocha

**URL:** <https://www.ruby-forum.com/t/using-rspec-and-mocha/68358>\
**Category:** Rails\
**Created:** [September 12, 2006, 1:07am UTC](https://www.ruby-forum.com/t/using-rspec-and-mocha/68358 "2006-09-12T01:07:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [September 12, 2006, 1:07am UTC](https://www.ruby-forum.com/t/using-rspec-and-mocha/68358/1 "2006-09-12T01:07:26Z")

</div>

I prefer to use rspec than test::unit for developing my apps. However  
I’ve found its mocking library to be pretty inflexible compared to  
Mocha. This isn’t really a surprise since rspec isn’t intended as a  
mocking framework, whereas Mocha is. So I’d like to play to both of  
their strengths and use rspec as my testing/specification framework  
and Mocha do to mocks.

I installed the plugins for each, and whenever I try to mock a method  
with Mocha I get a nil object error at  
plugins/mocha/lib/stubba/setup\_and\_teardown.rb at line 18. I’ve  
looked through all the files referenced in the trace and haven’t been  
able to figure anything out.

Not sure if anyone else has tried to use rspec and mocha together yet.  
I’d like some help from people with experience in either in getting  
these going.

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [September 12, 2006, 10:48am UTC](https://www.ruby-forum.com/t/using-rspec-and-mocha/68358/2 "2006-09-12T10:48:26Z")

</div>

Hi Pat,

Thanks for the positive feedback on Mocha. You might like to know that  
the  
RSpec developers agree with you and are looking to ditch their internal  
mocking framework and integrate properly with Mocha. We aren’t using  
Mocha  
with RSpec, but I know some people are (e.g.  
[http://www.lukeredpath.co.uk/](http://www.lukeredpath.co.uk/)).

My colleague Chris R. ([http://blog.seagul.co.uk/](http://blog.seagul.co.uk/)) has just looked into  
the  
error you are seeing and it looks like the problem stems from a call to  
Test::Unit::TestCase#add\_assertion from within  
Mocha::AutoVerify#teardown\_mocks. Apparently the RSpec descendant of  
Test::Unit::TestCase raises an exception when you call #add\_assertion  
(not  
sure why).

A possible quick fix would be to add a rescue in the Mocha code…

def teardown\_mocks  
mocks.each { |mock| mock.verify { add\_assertion rescue nil } }  
reset\_mocks  
end

This will mean that verified expectations will not be included in the  
assertion count, but I don’t think there are any other ramifications on  
the  
Mocha side. You might however want to check if it breaks anything on the  
RSpec side.

–  
James.

> **[James Mead](https://jamesmead.org/)**
>
> Ruby Developer, author of Mocha, and ex-Antarctic Hero.

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [September 12, 2006, 11:40am UTC](https://www.ruby-forum.com/t/using-rspec-and-mocha/68358/3 "2006-09-12T11:40:30Z")

</div>

## I asked the question on the rspec mailing list on your behalf and got a response here - [http://rubyforge.org/pipermail/rspec-devel/2006-September/000672.html](http://rubyforge.org/pipermail/rspec-devel/2006-September/000672.html)

James.

> **[James Mead](https://jamesmead.org/)**
>
> Ruby Developer, author of Mocha, and ex-Antarctic Hero.

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [September 14, 2006, 8:10am UTC](https://www.ruby-forum.com/t/using-rspec-and-mocha/68358/4 "2006-09-14T08:10:11Z")

</div>

On 9/12/06, James M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> A possible quick fix would be to add a rescue in the Mocha code…
> 
> def teardown\_mocks  
> mocks.each { |mock| mock.verify { add\_assertion rescue nil } }  
> reset\_mocks  
> end
> 
> –  
> James.  
> [http://blog.floehopper.org](http://blog.floehopper.org)

Hi James,

Thanks for looking into this for me. With your fix, I managed to run  
specs fine. The one problem I’ve run into has to do with  
transactions. I’m getting the “WARNING: There is already a  
transaction in progress”…“WARNING: There is no transaction in  
progress” messages. That’s occurring because the transactions look  
like  
BEGIN  
BEGIN  
ROLLBACK  
ROLLBACK

If I remove the mocha plugin then it doesn’t happen. I looked through  
all the mocha code and there was nothing that mentioned a transaction.  
I turned off transactional\_fixtures in test\_helper, figuring mocha  
might somehow use that, but it didn’t change anything.

rspec ought to be handling all the transactions since it’s my testing  
framework. Any idea why the inclusion of mocha would try to start a  
new transaction?

Again, thanks for your help.

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [September 14, 2006, 2:13pm UTC](https://www.ruby-forum.com/t/using-rspec-and-mocha/68358/5 "2006-09-14T14:13:48Z")

</div>

On 14/09/06, Pat M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ROLLBACK  
> Again, thanks for your help.
> 
> Pat

Hmm. Mocha definitely doesn’t do anything with the database, let alone  
transactions. I imagine it must be something to do with Rails’ TestCase  
and  
transactional fixtures. I’m not in a position to investigate this  
properly  
at the moment, but I’ll try and have a look later today.

BTW - it might be better to move this thread over onto the Mocha list \<  
removed\_email\_address@domain.invalid\>

–  
James.

> **[James Mead](https://jamesmead.org/)**
>
> Ruby Developer, author of Mocha, and ex-Antarctic Hero.
