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 -- ------------------------------ 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 http://www.netuxo.co.uk ------------------------------
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.htmlHi 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 & tomorrow! 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! -- James. http://blog.floehopper.org -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/mocha-developer/attachments/20060914/12ba479b/attachment.html