[mocha-developer] using mocha with rspec

Dan North dan at tastapod.com
Mon Mar 12 17:17:37 EDT 2007

Hi folks.

I've just started using rspec and I have to say it's very nice. The 
thing is, I prefer mocha's mocking dialect. So I thought a simple 
require 'mocha' would set me up.

Unfortunately, rspec does all its goodness using do/end blocks in 
anonymous classes, so it wasn't quite that obvious.

Anyway, here is the incantation I ended up using in my equivalent of a 
test_helper.rb file that I include in all my spec files:

# replace rspec's mocks with mocha
require 'mocha'
module Spec
  module Mocks
    remove_method :mock
    remove_method :stub
    remove_method :verify_mocks if method_defined? :verify_mocks
    include Mocha::AutoVerify

Now I have working mock(), stub() and verify_mocks(). Not sure what 
other Mocha stuff I'm missing yet, but I'll let you know how I get on.

I noticed that Mocha::Standalone also references 
Mocha::SetupAndTeardown, but I didn't see anything in there that seemed 
relevant to rspec mocking.


More information about the mocha-developer mailing list