2

I need to mock an external API request as part of a new cucumber test. My usual practice is to first write the test that makes the call, and expect WebMock to complain about it. Then using the information that WebMock provides, stub the request.

But in this repo (which is new to me), WebMock does not intercept the request. It just times out. Even more curious, it behaves exactly as I expect in rspec (it intercepts my request attempt, and tells me how to mock it), but not in cucumber.

I DO have this in features\support\webmock.rb:

WebMock.disable_net_connect!(:allow_localhost => true)

I've also tried putting that same line in features\support\env.rb. Still allows the request.

I can figure out how to stub this another way, of course, but it bothers me that my tests could be making live calls if I make a mistake. Can anyone help?

UPDATE:

I've determined that (:allow_localhost => true) does indeed keep the external requests from being made, but it does not trigger the WebMock mechanism that shows the bad request (I assume by rescuing an exception), and instead the request simply times out. Still don't know why.

UPDATE 2:

I've found the problem. This repo uses a custom client gem to talk to another internal service. That gem rescues connection errors, and thus the webmock error gets swallowed.

Daiku
  • 1,237
  • 11
  • 20
  • I've seen this done where WebMock is enabled in the development environment. Where is the webmock gem loaded? Is it defined in the app's Gemfile, and is it restricted to the test environment? If not it should be! – ReggieB Mar 17 '15 at 15:56
  • The gem is loaded for development and test. But it runs fine in development (makes live web requests). My problem is with running the tests. It makes requests when it shouldn't. – Daiku Mar 17 '15 at 16:00
  • If you restrict WebMock to test only, you can configure it to work everywhere in the test environment, without having to add hacks like .disable_net_connect! – ReggieB Mar 17 '15 at 16:03
  • But I do need to allow_localhost. And I've verified that's the problem. If I leave that line out, I get the expected behavior. But other stuff fails because I need that local endpoint to work. Also, if I ENABLE net_connect, my external requests succeed, not time out. So webmock IS stopping the requests - just not throwing the error. – Daiku Mar 17 '15 at 16:15

0 Answers0