[mocha-developer] Net::HTTPResponse

James Mead jamesmead44 at gmail.com
Thu Sep 14 08:28:32 EDT 2006

On 13/09/06, Anselm Heaton <anselm at netuxo.co.uk> wrote:
> Hello :) I have a problem with the Net::HTTP library...
> The Net::HTTP library [1] uses a Net::HTTPResponse object for all it's
> responses from web servers. This class has many subclasses, such as
> HTTPSuccess, HTTPRedirecttion, etc.
> When obtaining a response, the library suggests to check what it is by
> testing
> the class of the returned object - using case/when or kind_of? (which it
> does
> internally)
> So I need a mock object which can pass for a Net::HTTPResponse. However I
> can't create a Net::HTTPResponse directly - the "initialize" method is not
> documented, and in the code is commented as "internal use only" - which
> suggests it might just change in the future without warning.
> The second, admitedly evil idea was to stub "kind_of?" but that doesn't
> seem
> to work with Mocha.
> I was left having to do this :
> class MySuccess < Net::HTTPSuccess
>   def initialise
>   end
> end
> ...
> success = MySuccess.new
> success.expects(:body).returns("this is the body")
> ....
> Which is a little bit frustrating - it's to avoid having to do this type
> of
> code I'm using Mocha in the first place !
> Any ideas ?
> Thanks for this great library btw!,
> Anselm
> [1] http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTP.html

Hi Anselm,

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 &

The problem with not being able to mock kind_of? 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
Mocha::Mock class...

  undef kind_of?

Then you should be able to mock it. I will fix this properly soon.

Hope this helps. BTW your company looks pretty cool!

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/mocha-developer/attachments/20060914/12ba479b/attachment.html 

More information about the mocha-developer mailing list