2

I have a problem with my vcr with webmock spec. I have all the time the same problem when i run rspec spec/

    .rvm/gems/ruby-2.1.3/gems/webmock-1.18.0/lib/webmock/http_lib_adapters/net_http.rb:114:in `request': Real HTTP connections are disabled. Unregistered request: POST https://somevalue@accounts.spotify.com/api/token with body 'grant_type=client_credentials' with headers {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Content-Length'=>'29', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Ruby'} (WebMock::NetConnectNotAllowedError)

You can stub this request with the following snippet:

stub_request(:post, "https://somevalue@accounts.spotify.com/api/token").
  with(:body => {"grant_type"=>"client_credentials"},
       :headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Content-Length'=>'29', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Ruby'}).
  to_return(:status => 200, :body => "", :headers => {})

============================================================
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/rest_client-1.8.2/lib/restclient/request.rb:186:in `block in transmit'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/webmock-1.18.0/lib/webmock/http_lib_adapters/net_http.rb:123:in `start_without_connect'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/webmock-1.18.0/lib/webmock/http_lib_adapters/net_http.rb:150:in `start'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/rest_client-1.8.2/lib/restclient/request.rb:182:in `transmit'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/rest_client-1.8.2/lib/restclient/request.rb:68:in `execute'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/rest_client-1.8.2/lib/restclient/request.rb:35:in `execute'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/rest_client-1.8.2/lib/restclient.rb:73:in `post'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/rspotify-1.9.0/lib/rspotify/connection.rb:27:in `authenticate'
    from /home/pnc/sites/spolisty/config/initializers/devise.rb:260:in `block in <top (required)>'
    from /home/pnc/.rvm/gems/ruby-2.1.3/bundler/gems/devise-a9d90503e903/lib/devise.rb:293:in `setup'
    from /home/pnc/sites/spolisty/config/initializers/devise.rb:3:in `<top (required)>'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/activesupport-4.2.0.rc1/lib/active_support/dependencies.rb:268:in `load'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/activesupport-4.2.0.rc1/lib/active_support/dependencies.rb:268:in `block in load'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/activesupport-4.2.0.rc1/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/activesupport-4.2.0.rc1/lib/active_support/dependencies.rb:268:in `load'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/railties-4.2.0.rc1/lib/rails/engine.rb:652:in `block in load_config_initializer'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/activesupport-4.2.0.rc1/lib/active_support/notifications.rb:166:in `instrument'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/railties-4.2.0.rc1/lib/rails/engine.rb:651:in `load_config_initializer'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/railties-4.2.0.rc1/lib/rails/engine.rb:616:in `block (2 levels) in <class:Engine>'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/railties-4.2.0.rc1/lib/rails/engine.rb:615:in `each'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/railties-4.2.0.rc1/lib/rails/engine.rb:615:in `block in <class:Engine>'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/railties-4.2.0.rc1/lib/rails/initializable.rb:30:in `instance_exec'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/railties-4.2.0.rc1/lib/rails/initializable.rb:30:in `run'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/railties-4.2.0.rc1/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /home/pnc/.rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/tsort.rb:226:in `block in tsort_each'
    from /home/pnc/.rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
    from /home/pnc/.rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/tsort.rb:418:in `block (2 levels) in each_strongly_connected_component_from'
    from /home/pnc/.rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/tsort.rb:427:in `each_strongly_connected_component_from'
    from /home/pnc/.rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/tsort.rb:417:in `block in each_strongly_connected_component_from'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/railties-4.2.0.rc1/lib/rails/initializable.rb:44:in `each'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/railties-4.2.0.rc1/lib/rails/initializable.rb:44:in `tsort_each_child'
    from /home/pnc/.rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/tsort.rb:411:in `call'
    from /home/pnc/.rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/tsort.rb:411:in `each_strongly_connected_component_from'
    from /home/pnc/.rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/tsort.rb:347:in `block in each_strongly_connected_component'
    from /home/pnc/.rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/tsort.rb:345:in `each'
    from /home/pnc/.rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/tsort.rb:345:in `call'
    from /home/pnc/.rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/tsort.rb:345:in `each_strongly_connected_component'
    from /home/pnc/.rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/tsort.rb:224:in `tsort_each'
    from /home/pnc/.rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/tsort.rb:205:in `tsort_each'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/railties-4.2.0.rc1/lib/rails/initializable.rb:54:in `run_initializers'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/railties-4.2.0.rc1/lib/rails/application.rb:352:in `initialize!'
    from /home/pnc/sites/spolisty/config/environment.rb:5:in `<top (required)>'
    from /home/pnc/sites/spolisty/spec/rails_helper.rb:3:in `require'
    from /home/pnc/sites/spolisty/spec/rails_helper.rb:3:in `<top (required)>'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1072:in `require'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1072:in `block in requires='
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1072:in `each'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1072:in `requires='
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/rspec-core-3.1.7/lib/rspec/core/configuration_options.rb:103:in `block in process_options_into'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/rspec-core-3.1.7/lib/rspec/core/configuration_options.rb:102:in `each'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/rspec-core-3.1.7/lib/rspec/core/configuration_options.rb:102:in `process_options_into'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/rspec-core-3.1.7/lib/rspec/core/configuration_options.rb:22:in `configure'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:95:in `setup'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:84:in `run'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:69:in `run'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:37:in `invoke'
    from /home/pnc/.rvm/gems/ruby-2.1.3/gems/rspec-core-3.1.7/exe/rspec:4:in `<top (required)>'
    from /home/pnc/.rvm/gems/ruby-2.1.3/bin/rspec:23:in `load'
    from /home/pnc/.rvm/gems/ruby-2.1.3/bin/rspec:23:in `<main>'
    from /home/pnc/.rvm/gems/ruby-2.1.3/bin/ruby_executable_hooks:15:in `eval'
    from /home/pnc/.rvm/gems/ruby-2.1.3/bin/ruby_executable_hooks:15:in `<main>'

What i try to solve this problem:

  1. Add c.ignore_localhost = true to my vcr config

  2. Add this code to my rails_helper.rb

    require 'webmock/cucumber' WebMock.disable_net_connect!(:allow_localhost => true)

  3. I try remove all test where i use vcr and then run rspec spec/ but still this error appear

  4. This error shows me that i can stub this request, but how can i do this?

Any idea how can i resolve this error?

Panczo
  • 424
  • 5
  • 20
  • 1
    It actually tells you how to stub the request. Copy the text from "stub_request... " to "...:headers => {})" into your rspec tests. https://github.com/bblimke/webmock – SteveTurczyn Dec 29 '14 at 14:47
  • @Panchzo You seem to be under the impression that since you are running tests, it is all local. Nope, the tests are posting a request to `https://somevalue@accounts.spotify.com/api/token`, and that http request needs to be stubbed as mentioned by the message. – Prakash Murthy Dec 29 '14 at 15:13
  • The whole purpose of VCR is to record a real HTTP request and then replay it when that exact same request is made again, so no manual stubbing is required. Could you please share your VCR configuration? – apeniche Dec 29 '14 at 16:01
  • @Panczo Even I am facing the same issue. How did you solve the problem – mintuhouse Jun 24 '15 at 08:24

2 Answers2

2
require 'webmock'
WebMock.allow_net_connect!

to very top of spec_helper.rb doesn't work when you use the spring gem which will be enabled by default in rails 4.1 to preload rspec It will raise a WebMock::NetConnectNotAllowedError and break the test suit

Use a initializers:

# File: config/initializers/webmock.rb
if Rails.env.test?
  require 'webmock'
  WebMock.disable_net_connect!(allow_localhost: true)
end
lokeshjain2008
  • 1,879
  • 1
  • 22
  • 36
2

VCR should chime in when you run any spec that is making a HTTP request (whether or not you've wrapped it in a use_cassette block), so it looks like your config file is not getting picked up. For example, if you have required your vcr config file, but haven't wrapped your test spec in a use_cassette block, you will get some output like:

VCR::Errors::UnhandledHTTPRequestError:
An HTTP request has been made that VCR does not know how to handle:

There is currently no cassette in use. There are a few ways
   you can configure VCR to handle this request:

If your config file is spec/spec_vcr.rb make sure to add:

require 'spec_vcr'

to your spec/spec_helper.rb or spec/rails_helper.rb file. Make sure not to call your config file spec/vcr.rb or there will be conflicts with the actual gem.

littleforest
  • 2,057
  • 21
  • 29