-2

当我运行测试时,我收到一个错误:

/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:我想使用webmockgem 为 api 请求编写规范。但我不需要请求travis-ci并希望将这些请求添加到“忽略”。

4

1 回答 1

2

它解决了我的问题:

WebMock.disable_net_connect!(:allow => 'api.travis-ci.org')
于 2016-06-09T09:48:02.643 回答