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?







October 8th, 2009 at 11:01 pm
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.
January 29th, 2010 at 7:25 am
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
February 26th, 2010 at 1:58 pm
Thank you Rob for that comment – it stopped me banging my head against a brick wall
May 13th, 2010 at 4:50 pm
hi,really elegant Ipad,do you know where i can find that elegant.thanks,Vox