Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Ruby Programming Language

Mocking Net::HTTP


I am currently finishing up a release of Rannel, a library for  
sending sms messages through the open source SMS/WAP Gateway Kannel.

I have a couple of Unit tests that assure that the commands are send  
correctly to Kannel and all the tests are passing (i.e. it's  
working :) ).

Looking at the unit test code though, I realized, that I am violating  
one of the unit test premisses; The Independence of the Tests!

Therefore I thought that I could mock up the Net::HTTP requests done  
to Kannel, but my problem here is that I am not quite sure how I can  
do that, as the adapters (for GET and POST requests) actually use  
this package (Net::HTTP).

What would you recommend? Should I write a MockGet/Post adapter that  
will do the job? Or is there a transparent way to change the behavior  
of Net::HTTP without touching the adapters?

Thank you in advance,

Enrique Comba Riepenhausen

I'm pretty sure Flexmock can do this.
http://onestepback.org/software/flexmock/

Scan down to "Mocking Class Objects". I've used it to mock calls to my own
classes (but not Net::HTTP)

On 25/05/07, Enrique Comba Riepenhausen <eco@mac.com> wrote:

> What would you recommend? Should I write a MockGet/Post adapter that
> will do the job? Or is there a transparent way to change the behavior
> of Net::HTTP without touching the adapters?

You should be able to use Mocha (http://mocha.rubyforge.org) do do this.

Let me know if you have a specific test you need help with.
--
James.
http://blog.floehopper.org

You should be able to do something like this...

http://pastie.caboo.se/64679

Note that this is all stubs. If you want to assert that a particular
method is called you need to use Mock#expects instead of Mock#stubs.
If you want to assert that the method is called with particular
parameters, you can use the Expectation#with.

Also you might be able to use more real objects e.g. the response object...?

I hope this makes sense. Probably best to come over to the Mocha list
if you want more help...

http://rubyforge.org/mailman/listinfo/mocha-developer
--
James.
http://blog.floehopper.org

On 25 May 2007, at 23:40, James Mead wrote:

I checked your code and made a change in my test code taking that  
pastie (well the mock part of it) and commented out all the other  
tests... Run the tests and looked at the logs from kannel to see if  
there is any request hitting kannel...

And guess what?

Hey it works! :) :) :)

Thanks a million!

Enrique

No problem :-)
--
James.
http://blog.floehopper.org

On Sat, May 26, 2007 at 04:25:59AM +0900, Enrique Comba Riepenhausen wrote:
> I've taken a look at both frameworks and the following question  
> arises. If I am using my classes like the way I am using, how can I  
> possibly mockup Net::HTTP?

By overriding Net::HTTP's "new" method to return a new mock object which has
whatever behaviour you like. Or if you're using Net::HTTP.start, then
override that.
On May 26, 6:46 am, Brian Candler <B.Cand@pobox.com> wrote:

> On Sat, May 26, 2007 at 04:25:59AM +0900, Enrique Comba Riepenhausen wrote:
> > I've taken a look at both frameworks and the following question  
> > arises. If I am using my classes like the way I am using, how can I  
> > possibly mockup Net::HTTP?

> By overriding Net::HTTP's "new" method to return a new mock object which has
> whatever behaviour you like. Or if you're using Net::HTTP.start, then
> override that.

I need to develop a mockup server, so i can do performance testing
without needing the real server. I am only testing the client or the
Rails application so i do not need to use real server.

I need to override couple of Net::HTTP methods. Can you suggest which
framework is better to start with to create mockup objects which would
respond to the requests.

Thanks in advance

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc