[mocha-developer] Organizing tests and mocha expectations

James Mead jamesmead44 at gmail.com
Mon Oct 9 12:06:48 EDT 2006

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

# suggestion 1 (pretty much what I suggested to you before)
: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.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)
: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?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/mocha-developer/attachments/20061009/43c4eeed/attachment.html 

More information about the mocha-developer mailing list