2

我的录像机与 webmock 规范有问题。当我运行 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>'

我试图解决这个问题:

  1. 将 c.ignore_localhost = true 添加到我的 vcr 配置中

  2. 将此代码添加到我的 rails_helper.rb

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

  3. 我尝试删除我使用 vcr 的所有测试,然后运行 ​​rspec spec/ 但仍然出现此错误

  4. 这个错误告诉我我可以存根这个请求,但我该怎么做呢?

知道如何解决此错误吗?

4

2 回答 2

2
require 'webmock'
WebMock.allow_net_connect!

当您使用在 rails 4.1 中默认启用以预加载 rspec 的 spring gem 时,spec_helper.rb 的最顶部不起作用它会引发 aWebMock::NetConnectNotAllowedError并破坏测试服

使用初始化器:

# File: config/initializers/webmock.rb
if Rails.env.test?
  require 'webmock'
  WebMock.disable_net_connect!(allow_localhost: true)
end
于 2015-03-10T09:59:16.820 回答
2

当您运行任何发出 HTTP 请求的规范时(无论您是否已将其包装在一个use_cassette块中),VCR 都应该加入,因此看起来您的配置文件没有被拾取。例如,如果您需要您的 vcr 配置文件,但没有将您的测试规范包装在一个use_cassette块中,您将得到一些输出,例如:

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:

如果您的配置文件spec/spec_vcr.rb确保添加:

require 'spec_vcr'

到您的spec/spec_helper.rbspec/rails_helper.rb文件。确保不要调用您的配置文件spec/vcr.rb,否则会与实际的 gem 发生冲突。

于 2015-06-28T15:49:36.010 回答