Created
May 8, 2017 15:47
-
-
Save blaskovicz/0222b0dec84df670d96cc5bb53c9ca5e to your computer and use it in GitHub Desktop.
stubbing rails request object, mocking remote requests, and with mocha
This file contains 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
test 'should create account' do | |
user = User.find_by(provider: mock_user['provider'], uid: mock_user['uid']) | |
assert_nil user | |
# mock model remote updates | |
stub_request(:post, "#{fixtures(:some1).api_url}/destinations") | |
.to_return(status: 200, body: {}.to_json, headers: {}) | |
stub_request(:put, %r{#{fixtures(:some1).api_url}/destinations/\d+}) | |
.to_return(status: 204, body: {}.to_json, headers: {}) | |
target = session_create_url(provider: 'identity_api') | |
mock_request = ActionController::TestRequest.create() | |
mock_request.env['omniauth.auth'] = mock_user # eg : { 'uid' => 'prov|1234', 'provider' => 'auth0', 'info' => { 'email' => '[email protected]', ... } } | |
ApplicationController.any_instance.expects(:request).returns(mock_request).at_least_once | |
get target | |
assert_response :redirect | |
assert_equal 'http://some/location', @response.headers['location'] | |
user = User.find_by(provider: mock_user['provider'], uid: mock_user['uid']) | |
assert_not_nil user | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment