Created
October 30, 2015 01:36
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
request.env['REMOTE_ADDR'] = '1.2.3.4'
can be simplified toself.remote_addr = '127.0.0.1'