[mocha-developer] using mocha with rspec
dchelimsky at gmail.com
Tue Mar 13 07:55:13 EDT 2007
On 3/13/07, Dan North <dan at tastapod.com> wrote:
> Yes you're right - it doesn't automagically verify, but the verify_mocks
> method is in scope and I like to have that explicitly in my spec methods
> The big things on my wishlist are:
> 1. Matchers in with(..) like JMock has:
> expects(:chew).with(is_a(Chewable), ANYTHING) where is_a(..) is a
> [something] that responds to matches?(actual) with true/false, and
> ANYTHING is a matcher that always returns true. That way, you could use
> rspec's expectation matcher framework or roll your own matchers. (I've
> got a bunch I wrote for my own mocking framework - everyone should write
> a mocking framework at least twice once :)
> The method matches?(actual) seems fairly universal across these frameworks.
rspec's mocking framework uses matcher.matches?(actual) and use
matcher.description to build the failure message. If you use these
methods, then rspec users will be able to use rspec's matchers (which
are usable for both mock expectations and state
expectations/assertions) within mocha expectations in rspec. In fact,
test/unit users would be able to use them as well - they'd just have
to get the rspec gem and require 'spec/matchers' (which doesn't depend
on any other parts of rspec).
Hope I'm not being too intrusive here. I'd really love to see the day
where rspec need not support its own mocking framework. Making mocha a
first class option within rspec is a big first step towards this goal.
> 2. Type-aware mocks that fail with a NoMethodError if the underlying
> type doesn't respond to an invoked method or method_missing (see earlier
> 3. Something akin to after(another_mock, :method).
> None of these are showstoppers, but 1 is really high on my priorities
> and 2 and 3 would make my life happier. I might try to roll something
> for 1 if I get the time (read: unlikely!).
> James Mead 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.
> mocha-developer mailing list
> mocha-developer at rubyforge.org
More information about the mocha-developer