[mocha-developer] Net::HTTPResponse

Anselm Heaton anselm at netuxo.co.uk
Wed Sep 13 18:32:50 EDT 2006

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 

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
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!,

[1] http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTP.html

Netuxo Ltd
a workers' co-operative
providing low-cost IT solutions
for peace, environmental and social justice groups
and the radical NGO sector

Registered as a company in England and Wales. No 4798478
Registered office: 5 Caledonian Road, London N1 9DY, Britain
email office at netuxo.co.uk

More information about the mocha-developer mailing list