Skip to content

Instantly share code, notes, and snippets.

@dschneider
Created June 16, 2012 17:15
Show Gist options
  • Select an option

  • Save dschneider/2941985 to your computer and use it in GitHub Desktop.

Select an option

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

shlima commented Dec 15, 2014

Copy link
Copy Markdown

1️⃣

@melhotiby

Copy link
Copy Markdown

Rspec 3

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

@ilstar

ilstar commented Mar 16, 2017

Copy link
Copy Markdown

👍 thanks!

@gkalil

gkalil commented Mar 26, 2018

Copy link
Copy Markdown

I'm getting uninitialized constant ActionDispatch

@spacemunkay

spacemunkay commented Mar 20, 2019

Copy link
Copy Markdown

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
Copy Markdown

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