我的vcr与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>'
我试图解决这个问题:
将c.ignore_localhost = true添加到我的vcr配置
将此代码添加到我的rails_helper.rb
要求&#39; webmock / cucumber&#39; WebMock.disable_net_connect!(:allow_localhost =&gt; true)
我尝试删除所有使用vcr的测试,然后运行rspec spec /但仍出现此错误
此错误显示我可以存根此请求,但我该怎么做?
我知道如何解决此错误?
答案 0 :(得分: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.rb
或spec/rails_helper.rb
文件。确保不要调用配置文件spec/vcr.rb
,否则会与实际的gem发生冲突。
答案 1 :(得分:1)
require 'webmock'
WebMock.allow_net_connect!
到spec_helper.rb的顶部当你使用spring gem时将无法工作,这将在rails 4.1中默认启用以预加载rspec
它将引发WebMock::NetConnectNotAllowedError
并打破测试诉讼
使用初始化程序:
# File: config/initializers/webmock.rb
if Rails.env.test?
require 'webmock'
WebMock.disable_net_connect!(allow_localhost: true)
end