On 09/10/06, <b class="gmail_sendername">Bryan Helmkamp</b> <<a href="mailto:bhelmkamp@gmail.com">bhelmkamp@gmail.com</a>> wrote:<div><span class="gmail_quote"></span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Yes, that's exactly what I need! I need to stub a method in the<br>setup, and then replace the stub with expectations in specific<br>tests/specs. Either that, or stub the method and then be able to<br>provide expectations for the stub. There's a slight difference
<br>between the two, but, at least initially, the second way sounds better<br>because I wouldn't have to duplicate the stubbed return values in the<br>expectation.<br><br>How hard would this be to get into Mocha? Would it be likely to break
<br>anyone's tests?</blockquote><div><br> </div>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...
<br><br><span style="font-family: courier new,monospace;"># suggestion 1 (pretty much what I suggested to you before)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
object.stubs(:expected_method).with(:parameter1, :parameter2).returns(:result)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">object.expects(:expected_method).with(:parameter1, :parameter2).returns(:result) # replaces stubbing expectation
</span><br></div><br>note: in order to preserve the ability to set multiple expectations for the same method with different parameters, the <span style="font-family: courier new,monospace;">stubs()</span> call will need to have exactly the same
<span style="font-family: courier new,monospace;">with()</span> call, so that the specific expectation can be identified and replaced.<br><br><span style="font-family: courier new,monospace;"># suggestion 2</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">object.stubs(:expected_method).returns(:result)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
object.expects(:expected_method, :replace => true).with(:parameter1, :parameter2).returns(:result) # replaces stubbing expectation</span><br style="font-family: courier new,monospace;"><br>note: setting <span style="font-family: courier new,monospace;">
:replace => true</span> causes expectation to replace all existing stubbing or asserting expectations for the given method name (whatever their expected parameters).<br><br><span style="font-family: courier new,monospace;">
# suggestion 3</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">call_to_expected_method = object.stubs(:expected_method).returns(:result)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">
expect(call_to_expected_method).with(:parameter1, :parameter2).returns(:result) # replaces stubbing expectation</span><br style="font-family: courier new,monospace;"><br>So far, I'm leaning towards suggestion 3 or something similar.
<span style="font-family: courier new,monospace;">stubs()</span> and <span style="font-family: courier new,monospace;">expects()</span> 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.
<br>
<br>Thoughts anyone?<br clear="all"><br>-- <br>James.<br><a href="http://blog.floehopper.org">http://blog.floehopper.org</a>