[mocha-developer] Mocha, Stubba and RSpec

David Chelimsky dchelimsky at gmail.com
Wed Oct 25 19:40:08 EDT 2006


On 10/25/06, Ed Howland <ed.howland at gmail.com> wrote:
> On 10/25/06, James Mead <jamesmead44 at gmail.com> wrote:
> > Hi Ed,
> >
> > Unfortunately I haven't had much time to work on Mocha recently. The work I
> > have done most recently has been to make Mocha useable in the absence of
> > Test::Unit. This should help with integrating with the more recent
> > incarnations of RSpec. Take a look at the standalone_acceptance_test.rb file
> > in HEAD (revision 70) for some ideas on how to hook into Mocha from a non
> > Test::Unit testing framework. This is still a bit of a work in progress, but
> > you might be able to get somewhere with it.
> >
> > --
> Thanks, James
>
> I just gave a talk on Mocha and Stubba last night at the St. Louis
> Ruby Group. All my code examples were in RSpec, except for the last
> one which used stub(), which I generated in a normal
> Test::Unit::TestCase.
>
> I'll check out the HEAD revision and see how to do it.
>
> David C. on the RSpec team said in a comment on Jay Fields' blog that
> they were going to make RSpec pluggable for other mock frameworks,
> like yours and FlexMock.

Yes, it's true, but this is going to be a little further down - a few
weeks or a month. Right now we're busy stabilizing some things
including our own mock framework.

I'm not exactly sure how the integration will work, but I imagine that
there will be a hook into the runner that lets a framework say "use
me", at which point each spec will call the framework at the beginning
and the end. So as a user of  rspec and mocha, you would do something
like this:

require 'mocha/rspec_plugin'

As a writer of a framework like mocha, you'd say something like this:

RSPEC.use_me :mock, Mocha::RspecPlugin.new
#need to tell rspec to turn off its own mocking, hence :mock

module Mocha
  class RspecPlugin
    def spec_started(spec)
      #define methods here on the submitted spec like mock() and stub()
      (class << spec; self; end).class_eval { include Mocha::MockMethods }
    end

    def spec_ended(spec)
      #verify and clean up any mocks/stubs created since the spec started
    end
  end
end

This saves the user from having to things like this:

mock = Mocha::Mock.new()

and

mock.verify

Also helps to avoid collisions on the stuff both frameworks add to Object.

Maybe a simpler way would be to just have a flag that lets you turn of
rspec mocks in any given context:

require 'mocha'
context "blah" do
  rspec_mocks :off
  ...

That would require no integration points, but would not notify mocha
when to verify (so users would have to do so explicitly)

James - does any of this sound sound? I'm completely open to whatever
makes integration and use simple w/o requiring complex dependencies
between the frameworks.

Cheers,
David


> BTW, did you see how FlexMock is copying you
> now? Or at least jMock.
>
>
> Ed
> > James.
> > http://blog.floehopper.org
> > _______________________________________________
> > mocha-developer mailing list
> > mocha-developer at rubyforge.org
> > http://rubyforge.org/mailman/listinfo/mocha-developer
> >
> >
>
>
> --
> Ed Howland
> http://greenprogrammer.blogspot.com
> _______________________________________________
> mocha-developer mailing list
> mocha-developer at rubyforge.org
> http://rubyforge.org/mailman/listinfo/mocha-developer
>


More information about the mocha-developer mailing list