<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">
<br><div><div>On Jul 9, 2007, at 5:42 PM, David Chelimsky wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><blockquote type="cite"><p style="margin: 0.0px 0.0px 0.0px 10.0px; font: 12.0px Helvetica; min-height: 14.0px"><br></p> <p style="margin: 0.0px 0.0px 0.0px 10.0px"><font face="Helvetica" size="3" style="font: 12.0px Helvetica">As opposed to getting into complicated mocking scenarios, consider</font></p> <p style="margin: 0.0px 0.0px 0.0px 10.0px"><font face="Helvetica" size="3" style="font: 12.0px Helvetica">abstracting this logic into your model:</font></p> <p style="margin: 0.0px 0.0px 0.0px 10.0px"><font face="Helvetica" size="3" style="font: 12.0px Helvetica"><a href="http://blog.caboo.se/articles/2007/6/19/rspec-notes-from-the-trenches-2">http://blog.caboo.se/articles/2007/6/19/rspec-notes-from-the-trenches-2</a></font></p> </blockquote><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px"><br></p> <p style="margin: 0.0px 0.0px 0.0px 0.0px"><font face="Helvetica" size="3" style="font: 12.0px Helvetica">Agreed that this is a good approach in terms of dealing w/ rails</font></p> <p style="margin: 0.0px 0.0px 0.0px 0.0px"><font face="Helvetica" size="3" style="font: 12.0px Helvetica">models. </font></p></blockquote><div><br class="webkit-block-placeholder"></div><div>I generally agree with this, no doubt. But when the it comes to big apps and bigger test/spec suites, all that database communication (and fixtures creation/teardown), slows testruns downs and that lowers the chances of people running them regularly. </div><div>There's of course many different ways of dealing with this (another I'm doing is reworking the way fixtures work), but in a way I see it the same way as I do with, say, XML-RPC models (or REST, or other networking related models) that I maybe I don't really need to test communication with the network backend all the time, but more interested in that my model implementation behave like I expect them to when it comes to sending data to the client. Of course this starts to turn into a slightly slippery slope when it comes to rails since it relies so much its direct database mapping.</div><div><br class="webkit-block-placeholder"></div><div>I'm still trying this approach out along with other things to cut down specs runtime , and I eagerly await the day it bites me in the arse. :)</div><br><blockquote type="cite"><p style="margin: 0.0px 0.0px 0.0px 0.0px"><font face="Helvetica" size="3" style="font: 12.0px Helvetica">As for the question of isolating specific key/value pairs in a</font></p> <p style="margin: 0.0px 0.0px 0.0px 0.0px"><font face="Helvetica" size="3" style="font: 12.0px Helvetica">hash, you could do this:</font></p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px"><br></p> <p style="margin: 0.0px 0.0px 0.0px 0.0px"><font face="Helvetica" size="3" style="font: 12.0px Helvetica">obj.should_receive(:message) do |*args|</font></p> <p style="margin: 0.0px 0.0px 0.0px 0.0px"><font face="Helvetica" size="3" style="font: 12.0px Helvetica"><span class="Apple-converted-space"> </span>hash = args.pop</font></p> <p style="margin: 0.0px 0.0px 0.0px 0.0px"><font face="Helvetica" size="3" style="font: 12.0px Helvetica"><span class="Apple-converted-space"> </span>hash[interesting_key].should == expected_value</font></p> <p style="margin: 0.0px 0.0px 0.0px 0.0px"><font face="Helvetica" size="3" style="font: 12.0px Helvetica">end</font></p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px"><br></p> <p style="margin: 0.0px 0.0px 0.0px 0.0px"><font face="Helvetica" size="3" style="font: 12.0px Helvetica">It's not pretty, but it works.</font></p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px"><br></p> <p style="margin: 0.0px 0.0px 0.0px 0.0px"><font face="Helvetica" size="3" style="font: 12.0px Helvetica">The other thing you might consider is a custom matcher. Something like:</font></p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px"><br></p> <p style="margin: 0.0px 0.0px 0.0px 0.0px"><font face="Helvetica" size="3" style="font: 12.0px Helvetica">obj.should_receive(:message).with(has_key_value(key, value))</font></p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px"><br></p> <p style="margin: 0.0px 0.0px 0.0px 0.0px"><font face="Helvetica" size="3" style="font: 12.0px Helvetica">The mock framework can use these just as the expectation framework -</font></p> <p style="margin: 0.0px 0.0px 0.0px 0.0px"><font face="Helvetica" size="3" style="font: 12.0px Helvetica">though mocks rely on == instead of matches.</font></p> </blockquote><br></div><div>Cheers, I didn't know that you could do custom matchers for those kind of expectations too.</div><div><br class="webkit-block-placeholder"></div><div>JS</div><div><br class="webkit-block-placeholder"></div><br></body></html>