Mocking request.remote_ip for testing.

Subscribe to Mocking request.remote_ip for testing. 3 posts, 2 voices

 
Avatar clem_c_rock 19 posts

Hello,

I'm writing test for some geo-location methods which are all very IP dependent.

Is there a good way to mock a request.remote_ip?

Thanks for any help,

Clem C

 
Avatar clem_c_rock 19 posts

I found two solutions that worked nicely for me

1. @request.stubs(:remote_ip).returns(“1.2.3.4”) #using Mocha

2. def setup @controller = FooController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new

@request.instance_eval do
     def remote_ip
       "666.13.37.911" 
     end
   end
end
 
Avatar Jeff Cohen 89 posts

Clem,

Interesting. And if you prefer, you can avoid the instance_eval with this odd-looking syntax:

def @request.remote_ip
  "666.13.37.911" 
end

Pretty scary IP, by the way :-)