Require webmock/cucumber or rspec not working #99

Closed
zedtux opened this Issue May 28, 2011 · 11 comments

Comments

Projects
None yet
7 participants
@zedtux

zedtux commented May 28, 2011

Hello,

I'm starting a new rails project (Rails 3.0.7) with ruby 1.9.2(ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.6.0]).
I'm also using Rspec (2.6.0) and Cucumber (0.10.3) with WebMock (1.6.4).

I have followed the documentation and updated my features/support/env.rb file by adding this:

require 'webmock/cucumber'
require 'spec/support/webmocks'

and my spec/spec_helper.rb file by adding:

require 'webmock/rspec'

and I created a new file spec/support/webmocks.rb that contains my stub_request.

When I start a spec of a feature, I got the message:

Real HTTP connections are disabled. Unregistered request: GET https://...

You can stub this request with the following snippet:

...

But when I replace from the file features/support/env.rb and spec/spec_helper.rb lines

require 'webmock/...'

by

require 'webmock'
include WebMock::API

Then it works.

Did I something wrong?

@jcf

This comment has been minimized.

Show comment Hide comment
@jcf

jcf May 28, 2011

Collaborator

I can verify that following the instructions in the read me works for me and a Rails 3 + Ruby 1.9.2 app I've built recently. You only need to include WebMock::API directly if you're using WebMock outside of Test::Unit, Cucumber or RSpec.

It sounds like the #stub_request you're using doesn't match the HTTP request you want it to. Please post the an example of the request you're sending and the #stub_request you're using on the webmock-users google group as this sounds like an integration/implementation issue rather than a issue with the WebMock library itself.

Collaborator

jcf commented May 28, 2011

I can verify that following the instructions in the read me works for me and a Rails 3 + Ruby 1.9.2 app I've built recently. You only need to include WebMock::API directly if you're using WebMock outside of Test::Unit, Cucumber or RSpec.

It sounds like the #stub_request you're using doesn't match the HTTP request you want it to. Please post the an example of the request you're sending and the #stub_request you're using on the webmock-users google group as this sounds like an integration/implementation issue rather than a issue with the WebMock library itself.

@jcf jcf closed this May 28, 2011

@zedtux

This comment has been minimized.

Show comment Hide comment
@zedtux

zedtux May 28, 2011

I can verify that following the instructions in the read me works for me and a Rails 3 + Ruby 1.9.2 app I've built recently.

I will give it a try on my side like you did in order to check and I will back to you.

It sounds like the #stub_request you're using doesn't match the HTTP request you want it to.

The odd thing is that when I include the WebMock::API, all my #stub_request are working fine.

zedtux commented May 28, 2011

I can verify that following the instructions in the read me works for me and a Rails 3 + Ruby 1.9.2 app I've built recently.

I will give it a try on my side like you did in order to check and I will back to you.

It sounds like the #stub_request you're using doesn't match the HTTP request you want it to.

The odd thing is that when I include the WebMock::API, all my #stub_request are working fine.

@lichtamberg

This comment has been minimized.

Show comment Hide comment
@lichtamberg

lichtamberg Nov 4, 2011

I have the same problem...

Ruby 1.9.2-p290, Rails 3.1.1

 ActionView::Template::Error:
   Real HTTP connections are disabled. Unregistered request: POST http://127.0.0.1:1234/ with body '{"id":"jsonrpc","method":"getblockcount","params":[]}' with headers {'Accept'=>'*/*', 'Connection'=>'keep-alive', 'Content-Type'=>'application/json', 'Keep-Alive'=>'30', 'User-Agent'=>'Ruby'}

   You can stub this request with the following snippet:

   stub_request(:post, "http://127.0.0.1:1234/").
     with(:body => "{\"id\":\"jsonrpc\",\"method\":\"getblockcount\",\"params\":[]}",
          :headers => {'Accept'=>'*/*', 'Connection'=>'keep-alive', 'Content-Type'=>'application/json', 'Keep-Alive'=>'30', 'User-Agent'=>'Ruby'}).
     to_return(:status => 200, :body => "", :headers => {})

   registered request stubs:

   stub_request(:post, "http://127.0.0.1:1234/").
     with(:body => "{\"id\":\"jsonrpc\",\"method\":\"getblockcount\",\"params\":[]}",
          :headers => {'Accept'=>'*/*', 'Connection'=>'keep-alive', 'Content-Type'=>'application/json', 'Keep-Alive'=>'30', 'User-Agent'=>'Ruby'})

   ============================================================

And in my code:

stub_request(:post, "http://127.0.0.1:1234/").
     with(:body => "{\"id\":\"jsonrpc\",\"method\":\"getblockcount\",\"params\":[]}",
          :headers => {'Accept'=>'*/*', 'Connection'=>'keep-alive', 'Content-Type'=>'application/json', 'Keep-Alive'=>'30', 'User-Agent'=>'Ruby'}).
     to_return(:status => 200, :body => '{"result":40944,"error":null,"id":"jsonrpc"}', :headers => {})

I have the same problem...

Ruby 1.9.2-p290, Rails 3.1.1

 ActionView::Template::Error:
   Real HTTP connections are disabled. Unregistered request: POST http://127.0.0.1:1234/ with body '{"id":"jsonrpc","method":"getblockcount","params":[]}' with headers {'Accept'=>'*/*', 'Connection'=>'keep-alive', 'Content-Type'=>'application/json', 'Keep-Alive'=>'30', 'User-Agent'=>'Ruby'}

   You can stub this request with the following snippet:

   stub_request(:post, "http://127.0.0.1:1234/").
     with(:body => "{\"id\":\"jsonrpc\",\"method\":\"getblockcount\",\"params\":[]}",
          :headers => {'Accept'=>'*/*', 'Connection'=>'keep-alive', 'Content-Type'=>'application/json', 'Keep-Alive'=>'30', 'User-Agent'=>'Ruby'}).
     to_return(:status => 200, :body => "", :headers => {})

   registered request stubs:

   stub_request(:post, "http://127.0.0.1:1234/").
     with(:body => "{\"id\":\"jsonrpc\",\"method\":\"getblockcount\",\"params\":[]}",
          :headers => {'Accept'=>'*/*', 'Connection'=>'keep-alive', 'Content-Type'=>'application/json', 'Keep-Alive'=>'30', 'User-Agent'=>'Ruby'})

   ============================================================

And in my code:

stub_request(:post, "http://127.0.0.1:1234/").
     with(:body => "{\"id\":\"jsonrpc\",\"method\":\"getblockcount\",\"params\":[]}",
          :headers => {'Accept'=>'*/*', 'Connection'=>'keep-alive', 'Content-Type'=>'application/json', 'Keep-Alive'=>'30', 'User-Agent'=>'Ruby'}).
     to_return(:status => 200, :body => '{"result":40944,"error":null,"id":"jsonrpc"}', :headers => {})
@lichtamberg

This comment has been minimized.

Show comment Hide comment
@lichtamberg

lichtamberg Nov 4, 2011

I use the HTTPPersistent gem.. Maybe this is the problem?

I use the HTTPPersistent gem.. Maybe this is the problem?

@zedtux

This comment has been minimized.

Show comment Hide comment
@zedtux

zedtux Nov 5, 2011

I don't use that gem in my project.

zedtux commented Nov 5, 2011

I don't use that gem in my project.

@bigtruckdriver22

This comment has been minimized.

Show comment Hide comment
@bigtruckdriver22

bigtruckdriver22 Nov 13, 2013

FYI I had the same problem as OP.
When I changed my cucumber env.rb file to use:

require 'webmock'
include WebMock::API

Then the stubs started to be picked up properly. Struggled with this for quite some time.
Normally if your stubs are incorrectly formed you see the message:

registered request stubs: ......

But until I made the above change, I would not get that message, yet I could still see the stubs in the registry if I ran the following:

puts WebMock::StubRegistry.instance.request_stubs

My environment was as follows
Capybara 2.1.0, WebMock 1.13.0, Rails 4.0.0, ruby 1.9.3p448, mocha 0.14.0, cucumber 1.40
Hopefully fix can be found, something to do with how the WebMock API is being loaded.

FYI I had the same problem as OP.
When I changed my cucumber env.rb file to use:

require 'webmock'
include WebMock::API

Then the stubs started to be picked up properly. Struggled with this for quite some time.
Normally if your stubs are incorrectly formed you see the message:

registered request stubs: ......

But until I made the above change, I would not get that message, yet I could still see the stubs in the registry if I ran the following:

puts WebMock::StubRegistry.instance.request_stubs

My environment was as follows
Capybara 2.1.0, WebMock 1.13.0, Rails 4.0.0, ruby 1.9.3p448, mocha 0.14.0, cucumber 1.40
Hopefully fix can be found, something to do with how the WebMock API is being loaded.

@jwaterfaucett

This comment has been minimized.

Show comment Hide comment
@jwaterfaucett

jwaterfaucett Jan 25, 2014

Confirming original posters and @bigtruckdriver22 problem. Same here for me.

Platform info: rubies ( 2.0.0-p247, 1.9.3-p448, 1.9.3-p194) cucumber 1.3.10, and WebMock 1.17.1

Confirming original posters and @bigtruckdriver22 problem. Same here for me.

Platform info: rubies ( 2.0.0-p247, 1.9.3-p448, 1.9.3-p194) cucumber 1.3.10, and WebMock 1.17.1

@jarinudom

This comment has been minimized.

Show comment Hide comment
@jarinudom

jarinudom Feb 28, 2014

Same problem here, I was tearing my hair out trying to figure out why it wasn't working until I saw this.

Same problem here, I was tearing my hair out trying to figure out why it wasn't working until I saw this.

@bblimke

This comment has been minimized.

Show comment Hide comment
@bblimke

bblimke Mar 2, 2014

Owner

I just generated a new sample rails app Ruby 2.0. cucumber-rails 1.4.0, capybara 2.2.1. WebMock 1.17.3 to reproduce that isse.

I created a sample cucumber scenario that invokes a controller action using cucumber. Everything worked fine here. The unstubbed http request invoked in controller action was intercepted by WebMock and correct error message is printed with instructions.

I'm not able to reproduce the issue.

In order to try to fix it, I would need someone to prepare a failing example. I.e generate a sample rails app and with one cucumber scenario that reproduces that issue.

Owner

bblimke commented Mar 2, 2014

I just generated a new sample rails app Ruby 2.0. cucumber-rails 1.4.0, capybara 2.2.1. WebMock 1.17.3 to reproduce that isse.

I created a sample cucumber scenario that invokes a controller action using cucumber. Everything worked fine here. The unstubbed http request invoked in controller action was intercepted by WebMock and correct error message is printed with instructions.

I'm not able to reproduce the issue.

In order to try to fix it, I would need someone to prepare a failing example. I.e generate a sample rails app and with one cucumber scenario that reproduces that issue.

@jarinudom

This comment has been minimized.

Show comment Hide comment
@jarinudom

jarinudom Mar 2, 2014

For me, at least, the issue was not that it wasn't blocking requests properly. It was that it was still failing even if you had stubbed that request (even if you used the exact code that it recommended).

I did notice that it wouldn't show ANY stubs being registered, so it's probably not registering them at all.

For me, at least, the issue was not that it wasn't blocking requests properly. It was that it was still failing even if you had stubbed that request (even if you used the exact code that it recommended).

I did notice that it wouldn't show ANY stubs being registered, so it's probably not registering them at all.

@bblimke

This comment has been minimized.

Show comment Hide comment
@bblimke

bblimke Mar 2, 2014

Owner

I just declared a stub in a cucumber step, then invoked another step that called controller action that invoked an http request and it worked fine.
What capybara driver do you use?
What rack server do you use?

Owner

bblimke commented Mar 2, 2014

I just declared a stub in a cucumber step, then invoked another step that called controller action that invoked an http request and it worked fine.
What capybara driver do you use?
What rack server do you use?

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