[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Mocking Net::HTTP

Enrique Comba Riepenhausen

5/25/2007 4:45:00 PM

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

7 Answers

Brian Candler

5/25/2007 6:04:00 PM

0

On Sat, May 26, 2007 at 01:45:06AM +0900, Enrique Comba Riepenhausen wrote:
> 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?

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

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


James Mead

5/25/2007 6:10:00 PM

0

On 25/05/07, Enrique Comba Riepenhausen <ecomba@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.rub...) do do this.

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

James Mead

5/25/2007 9:40:00 PM

0

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

http://pastie.cabo...

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...
--
James.
http://blog.floe...

Enrique Comba Riepenhausen

5/25/2007 9:49:00 PM

0


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

> You should be able to do something like this...
>
> http://pastie.cabo...
>
> 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...
> --
> James.
> http://blog.floe...
>

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

James Mead

5/25/2007 9:56:00 PM

0

No problem :-)
--
James.
http://blog.floe...

Brian Candler

5/26/2007 1:47:00 PM

0

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.

pudur.ramaswamy

6/7/2007 6:10:00 PM

0

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