RSpec 和 WebMock:忽略请求
RSpec and WebMock: ignore request
当我 运行 测试时,我得到了这个错误:
/gems/ruby-2.3.1@app2/gems/webmock-2.1.0/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb:166:in `block in <class:TyphoeusAdapter>': Real HTTP connections are disabled. Unregistered request: HEAD https://api.travis-ci.org/ with headers {'Accept'=>'application/vnd.travis-ci.2+json', 'User-Agent'=>'Travis/1.8.2 (Mac OS X 10.11.4 like Darwin; Ruby 2.3.1-p112; RubyGems 2.5.1) Faraday/0.9.2 Typhoeus/0.8.0'} (WebMock::NetConnectNotAllowedError)
You can stub this request with the following snippet:
stub_request(:head, "https://api.travis-ci.org/").
with(:headers => {'Accept'=>'application/vnd.travis-ci.2+json', 'User-Agent'=>'Travis/1.8.2 (Mac OS X 10.11.4 like Darwin; Ruby 2.3.1-p112; RubyGems 2.5.1) Faraday/0.9.2 Typhoeus/0.8.0'}).
to_return(:status => 200, :body => "", :headers => {})
我就是这样做来解决这个问题的:
spec_helper.rb:
require 'webmock/rspec'
require 'capybara/rspec'
require 'factory_girl_rails'
WebMock.disable_net_connect!(allow_localhost: true)
RSpec.configure do |config|
config.before(:each) do
stub_request(:any, "https://api.travis-ci.org/")
.with(:headers => {'Accept'=>'application/vnd.travis-ci.2+json', 'User-Agent'=>'Travis/1.8.2 (Mac OS X 10.11.4 like Darwin; Ruby 2.3.1-p112; RubyGems 2.5.1) Faraday/0.9.2 Typhoeus/0.8.0'})
.to_return(:status => 200, :body => "", :headers => {})
end
end
但我仍然不断收到此错误,也许有人有解决方法?
UPD: 我想使用 webmock
gem 为 api 请求编写规范。但我不需要来自 travis-ci
的请求,并希望将这些请求添加到 "ignore".
它解决了我的问题:
WebMock.disable_net_connect!(:allow => 'api.travis-ci.org')
当我 运行 测试时,我得到了这个错误:
/gems/ruby-2.3.1@app2/gems/webmock-2.1.0/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb:166:in `block in <class:TyphoeusAdapter>': Real HTTP connections are disabled. Unregistered request: HEAD https://api.travis-ci.org/ with headers {'Accept'=>'application/vnd.travis-ci.2+json', 'User-Agent'=>'Travis/1.8.2 (Mac OS X 10.11.4 like Darwin; Ruby 2.3.1-p112; RubyGems 2.5.1) Faraday/0.9.2 Typhoeus/0.8.0'} (WebMock::NetConnectNotAllowedError)
You can stub this request with the following snippet:
stub_request(:head, "https://api.travis-ci.org/").
with(:headers => {'Accept'=>'application/vnd.travis-ci.2+json', 'User-Agent'=>'Travis/1.8.2 (Mac OS X 10.11.4 like Darwin; Ruby 2.3.1-p112; RubyGems 2.5.1) Faraday/0.9.2 Typhoeus/0.8.0'}).
to_return(:status => 200, :body => "", :headers => {})
我就是这样做来解决这个问题的:
spec_helper.rb:
require 'webmock/rspec'
require 'capybara/rspec'
require 'factory_girl_rails'
WebMock.disable_net_connect!(allow_localhost: true)
RSpec.configure do |config|
config.before(:each) do
stub_request(:any, "https://api.travis-ci.org/")
.with(:headers => {'Accept'=>'application/vnd.travis-ci.2+json', 'User-Agent'=>'Travis/1.8.2 (Mac OS X 10.11.4 like Darwin; Ruby 2.3.1-p112; RubyGems 2.5.1) Faraday/0.9.2 Typhoeus/0.8.0'})
.to_return(:status => 200, :body => "", :headers => {})
end
end
但我仍然不断收到此错误,也许有人有解决方法?
UPD: 我想使用 webmock
gem 为 api 请求编写规范。但我不需要来自 travis-ci
的请求,并希望将这些请求添加到 "ignore".
它解决了我的问题:
WebMock.disable_net_connect!(:allow => 'api.travis-ci.org')