[mocha-developer] using mocha with rspec

James Mead jamesmead44 at gmail.com
Tue Mar 13 05:26:22 EDT 2007

On 12/03/07, Dan North <dan at tastapod.com> wrote:
> 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
>   end
> end
> 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.

Hi Dan,

I'm glad you like Mocha's mocking dialect. It's great you've got Mocha
working with RSpec.

Mocha::Standalone was an early attempt at simplifying integration with other
testing frameworks prompted by a request from the RSpec community. Work on
this ground to a halt when RSpec developed their own mocking functionality.

Mocha::SetupAndTeardown is to do with what RSpec call partial mocks i.e.
where you are mocking a method on a concrete class or object. Looking at
what you've done, I suspect you will still be able to setup expectations
like this but (a) they won't be verified and (b) the concrete class or
object won't be returned to its original state (Mocha temporarily messes
with the method implementation).

Let us know if there are any changes we can make that would make the
integration easier.

More information about the mocha-developer mailing list