On 13/09/06, <b class="gmail_sendername">Anselm Heaton</b> <<a href="mailto:anselm@netuxo.co.uk">anselm@netuxo.co.uk</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;">
Hello :) I have a problem with the Net::HTTP library...<br><br>The Net::HTTP library [1] uses a Net::HTTPResponse object for all it's<br>responses from web servers. This class has many subclasses, such as<br>HTTPSuccess, HTTPRedirecttion, etc.
<br>When obtaining a response, the library suggests to check what it is by testing<br>the class of the returned object - using case/when or kind_of? (which it does<br>internally)<br><br>So I need a mock object which can pass for a Net::HTTPResponse. However I
<br>can't create a Net::HTTPResponse directly - the "initialize" method is not<br>documented, and in the code is commented as "internal use only" - which<br>suggests it might just change in the future without warning.
<br><br>The second, admitedly evil idea was to stub "kind_of?" but that doesn't seem<br>to work with Mocha.<br><br>I was left having to do this :<br>class MySuccess < Net::HTTPSuccess<br> def initialise<br> end
<br>end<br>...<br>success = MySuccess.new<br>success.expects(:body).returns("this is the body")<br>....<br><br>Which is a little bit frustrating - it's to avoid having to do this type of<br>code I'm using Mocha in the first place !
<br><br>Any ideas ?<br><br>Thanks for this great library btw!,<br>Anselm<br><br>[1] <a href="http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTP.html">http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTP.html
</a></blockquote><div><br>Hi Anselm,<br><br>Sorry I haven't got round to reading your tutorial in detail yet - things have been a bit hectic the last few days and I'm at RailsConf Europe today & tomorrow!<br><br>The problem with not being able to mock
<span style="font-family: courier new,monospace;">kind_of?</span> is a known one - there are a bunch of methods like this which I need to sort out. As a quick fix you could patch your Mocha installation by adding this line at the bottom of the
<span style="font-family: courier new,monospace;">Mocha::Mock</span> class...<br><br><span style="font-family: courier new,monospace;"> undef kind_of?</span><br style="font-family: courier new,monospace;"><br>Then you should be able to mock it. I will fix this properly soon.
<br><br>Hope this helps. BTW your company looks pretty cool!<br></div></div><br>-- <br>James.<br><a href="http://blog.floehopper.org">http://blog.floehopper.org</a>