Skip to content

Instantly share code, notes, and snippets.

@dschneider
Created June 16, 2012 17:15
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dschneider/2941985 to your computer and use it in GitHub Desktop.
Save dschneider/2941985 to your computer and use it in GitHub Desktop.
RSpec - Stub Remote IP Request
ActionDispatch::Request.any_instance.stub(:remote_ip).and_return("192.168.0.1")
@shlima
Copy link

shlima commented Dec 15, 2014

1️⃣

@melhotiby
Copy link

Rspec 3

allow_any_instance_of(ActionDispatch::Request).to receive(:remote_ip).and_return('192.168.0.1')

@ilstar
Copy link

ilstar commented Mar 16, 2017

👍 thanks!

@gkalil
Copy link

gkalil commented Mar 26, 2018

I'm getting uninitialized constant ActionDispatch

@spacemunkay
Copy link

spacemunkay commented Mar 20, 2019

A different workaround in rspec:

Rspec.configure do |config|
  config.before(:each, type: :controller) do
    @request.remote_addr = '127.0.0.1'
  end
end

@pjmartorell
Copy link

You should specify if you are talking about controller specs or requests specs, because every kind of test requires a different workaround. In this case I guess you are referring to controller tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment