[mocha-developer] Organizing tests and mocha expectations

Bryan Helmkamp bhelmkamp at gmail.com
Mon Oct 9 17:16:24 EDT 2006

I'm very new to Mocha and mocking in general, so I'm not ready to
offer up suggestions for syntax just yet.  I'll ponder these a bit
more when I have a chance.

One consideration I'd like to throw out there is the duplication of
the returned value (or, equivalently, the raised exception).  For my
original example, I don't see a need to re-specify the results for the
save!() and find() calls when I add my expectations.

What do you guys think?  I'll try to chime in with some more input
about the syntax soon.


On 10/9/06, James Mead <jamesmead44 at gmail.com> wrote:
> On 09/10/06, Bryan Helmkamp <bhelmkamp at gmail.com> wrote:
> > Yes, that's exactly what I need!  I need to stub a method in the
> > setup, and then replace the stub with expectations in specific
> > tests/specs.  Either that, or stub the method and then be able to
> > provide expectations for the stub.  There's a slight difference
> > between the two, but, at least initially, the second way sounds better
> > because I wouldn't have to duplicate the stubbed return values in the
> > expectation.
> >
> > How hard would this be to get into Mocha?  Would it be likely to break
> > anyone's tests?
>  I don't think it would be too hard to make the necessary changes (although
> there's a bit of a backlog of changes at the moment), but I'd like to spend
> a bit of time thinking about the best syntax for it. I've been chatting to
> my colleague Chris Roos and we've come up with a few different
> alternatives...
> # suggestion 1 (pretty much what I suggested to you before)
>  object.stubs(:expected_method).with(:parameter1,
> :parameter2).returns(:result)
> object.expects(:expected_method).with(:parameter1,
> :parameter2).returns(:result) # replaces stubbing expectation
> note: in order to preserve the ability to set multiple expectations for the
> same method with different parameters, the stubs() call will need to have
> exactly the same with() call, so that the specific expectation can be
> identified and replaced.
> # suggestion 2
>  object.stubs(:expected_method).returns(:result)
>  object.expects(:expected_method, :replace => true).with(:parameter1,
> :parameter2).returns(:result) # replaces stubbing expectation
> note: setting :replace => true causes expectation to replace all existing
> stubbing or asserting expectations for the given method name (whatever their
> expected parameters).
>  # suggestion 3
> call_to_expected_method =
> object.stubs(:expected_method).returns(:result)
>  expect(call_to_expected_method).with(:parameter1,
> :parameter2).returns(:result) # replaces stubbing expectation
> So far, I'm leaning towards suggestion 3 or something similar. stubs() and
> expects() and their fellow expectation builder methods already return the
> expectation object, so it wouldn't be hard to implement. I'm planning on
> taking a look at JMock to see whether they handle this kind of idea.
> Thoughts anyone?
> --
> James.
> http://blog.floehopper.org
> _______________________________________________
> mocha-developer mailing list
> mocha-developer at rubyforge.org
> http://rubyforge.org/mailman/listinfo/mocha-developer

http://www.MyCongress.org/ -- coming soon

More information about the mocha-developer mailing list