Quantcast
Channel: Ruby – My Programming Blog
Viewing all articles
Browse latest Browse all 12

How to stub an IP in Ruby on Rails, RSpec Version 3 ?

$
0
0

Often when you are writing unit tests, you want to mock your IP address. Some of the uses cases could be – you are checking if IP is within the certain range.

For RSpec 2.x  mocking  IP would look like this:

 ActionDispatch::Request.any_instance.stub(:remote_ip).and_return("192.168.0.1")

Taken from here

However, if you try to do it in RSpec 3.x, it will give you an error since ‘any_instance‘ syntax is deprecated.

To make the same work for RSpec 3.x use this:

   allow_any_instance_of(ActionDispatch::Request).to receive(:remote_ip).and_return("192.168.99.225")

Tagged: Rails, Rspec, Rspec 3, Rspec mock ip, Rspec Stub IP, Ruby, Ruby on Rails

Viewing all articles
Browse latest Browse all 12

Trending Articles