Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created October 30, 2015 01:36
Show Gist options
  • Save JoshCheek/f035b162a7761db1f096 to your computer and use it in GitHub Desktop.
Save JoshCheek/f035b162a7761db1f096 to your computer and use it in GitHub Desktop.
How to set the IP address for a test on a Rails app
# app/controllers/users_controller.rb
class UsersController < ApplicationController
def index
render text: "Your IP is #{request.remote_ip}"
end
end
# test/controllers/users_controller_test.rb
class UsersControllerTest < ActionController::TestCase
test "should get index" do
request.env['REMOTE_ADDR'] = '1.2.3.4'
response = get :index
assert_equal 'Your IP is 1.2.3.4', response.body
end
end
@artur-intech
Copy link

request.env['REMOTE_ADDR'] = '1.2.3.4' can be simplified to

self.remote_addr = '127.0.0.1'

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