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.

Pat

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/).

My colleague Chris R. (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.

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

James.

On 9/12/06, James M. [email protected] 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

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

On 14/09/06, Pat M. [email protected] 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 <
[email protected]>


James.