[mocha-developer] using mocha with rspec
dchelimsky at gmail.com
Tue Mar 13 07:47:33 EDT 2007
On 3/13/07, James Mead <jamesmead44 at gmail.com> wrote:
> 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.
Dan and James,
Thank you both for moving on this. I've been wanting to figure out an
easy way to let people choose their mock framework when they use
rspec, but there have been other priorities in rspec and limited
What I'd like to see is something that I can build into rspec such
that people can either choose to use mocha for an entire project by
putting a single declaration in a helper file with no monkey patching
(in fact, the structure Dan patched has already changed in rspec's
trunk), or by including a module in a given context/describe block.
If you guys can work together to make that happen, I'll make it a
first class citizen of rspec as soon as any relevant changes are
released in mocha.
> mocha-developer mailing list
> mocha-developer at rubyforge.org
More information about the mocha-developer