RSS feed

Nutsmuggling

Faking an IP with Webrat

Lately I have been diving into TDD, and more specifically BDD, thanks to the wonderful Cucumber testing framework.

Today I have had to test a specifical IP based feature. Simply put, I get the user IP and store it into the database.

The simple rails way to get a visitor IP address is

request.remote_ip

If you do this while developing you’ll usually get a predictable

127.0.0.1

How do I test that my controller puts a correct IP in the database?
At first I thought about creating a mock request object. But this way I would be testing only the saving portion of the method, not the part detecting the IP. In other words, I needed to simulate an IP address at a lower layer.

Webrat to the rescue! Thanks to the nice folks at the webrat irc channel I got an answer. Simply:

header('REMOTE_ADDR', ip_address)

The Webrat headermethod lets you set all the header variable of you session. Handy, isn’t it?

Share and Enjoy:
  • Digg
  • del.icio.us
  • Google
  • Technorati
  • StumbleUpon
  • Furl
  • Reddit

3 responses to “Faking an IP with Webrat”

  1. Rob says:

    Thank you I was wondering how to do that! I noticed that you have to make sure to use webrat’s ‘visit’ method instead of ‘get’ otherwise it won’t work.

  2. Deadra Ramaker says:

    Lots of of bloggers aren’t too happy with the new iPad.There was 2 much hoopla regarding it and lots of people got disapointed.You see, I can actually see lots of the cool potential of this gadget. Third-party apps for making music, games, newspapers and magazine and FFS books, all sorts of neat stuff, but they failed to sell it very well (excluding the books). It looks kinda incomplete

  3. Matthew says:

    Thank you Rob for that comment – it stopped me banging my head against a brick wall :)

Leave a Reply