[mocha-developer] Organizing tests and mocha expectations

David Chelimsky dchelimsky at gmail.com
Mon Oct 9 17:06:11 EDT 2006


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

-1
This makes sense while the two are right next to each other, but if
the first was in the setup and the second in a spec it might be
confusing - especially when you've got other specs that have different
params.

> 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

+1
This is nice because the stub syntax doesn't change and the fact that
the expectation is replacing a stub becomes explicit. Much less
confusion.

>
> 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

-1
I think this one leads to more confusion in the end.

Just my 3 cents (I guess -1 cent if you add them up).

Cheers,
David


>
> 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
>
>


More information about the mocha-developer mailing list