Using rspec and mocha

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.


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 (

My colleague Chris Roos ( 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 } }

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.

I asked the question on the rspec mailing list on your behalf and got a
response here -

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

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.


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