ruby-on-railsrubycucumbercapybarawebmock

Mocking login for remote services - Cucumber, Capybara, Rails


I have a Rails application that has changed from user authentication via devise to accessing a remote API for authentication. For the test suite, this requires that I mock the https request for authentication and return a valid response. We are using Cucumber and Capybara for testing and I am attempting to use the webmock Gem to mock the login response.

The login is initiated by a button click on a form (Capybara action 'click_button').

Unfortunately, although webmock appears to be installed correctly (I am able to make a Net::HTTP POST request and this is recognized by Webmock), the POST to the remote authorization facility is not being captured by webmock. I know that the form POST is making its way to the controller, because the stacktrace shows that the POST is being executed in the controller as it should and the error message is "Errno::ECONNREFUSED at ... Failed to open TCP connection".

I have tried:

WebMock.stub_request(:any, '127.0.0.1').to_return(body: {STATUSCODE: 1}.to_json)

and

WebMock::stub_request(:any, '127.0.0.1').to_return(body: {STATUSCODE: 1}.to_json)

and

stub_request(:any, '127.0.0.1').to_return(body: {STATUSCODE: 1}.to_json)

I have tried putting the stub_request call in the devise_steps.rb file, just before the "click_button" command, as well as in the features/support/env.rb and features/support/webmock.rb file.

I assume that what I am doing is so common that it has to be possible, but I have found nothing that indicates why the stub_request is not successful.


Solution

  • The root cause of the problem was that the path on the stub_request was not complete. I had misread the webmock documentation....