Mocking Net::HTTP

  • Thread starter Enrique Comba Riepenhausen
  • Start date
E

Enrique Comba Riepenhausen

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
 
B

Brian Candler

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/flexmock/

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

James Mead

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.
 
J

James Mead

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
 
E

Enrique Comba Riepenhausen

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

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
 
B

Brian Candler

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.
 
P

pudur.ramaswamy

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top