【问题标题】:RSpec and WebMock: ignore requestRSpec 和 WebMock:忽略请求
【发布时间】:2016-06-09 08:13:22
【问题描述】:

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

/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 的请求,我想将这些请求添加到“忽略”。

【问题讨论】:

  • 你能给我们介绍一下上下文吗?
  • 什么样的上下文?我有webmock gem,我想忽略来自 travis 的这个请求。我想我正确地添加了一个“规则”来忽略这个请求。
  • 我只是要求介绍一下您想要做什么,使用哪种技术以及您尝试了什么。因为您从“测试”开始,我不知道上下文是什么。将此更新应用到您的帖子中,人们会理解并且您会从社区中得到很多答案;)。
  • 你知道这些请求来自哪里吗? travis 不会在测试服中运行任何请求似乎是正确的,所以我很确定它们是在 "before(:each)" 钩子之前触发的;)
  • 这个请求来自travisgem

标签: ruby-on-rails ruby rspec webmock


【解决方案1】:

它解决了我的问题:

WebMock.disable_net_connect!(:allow => 'api.travis-ci.org')

【讨论】:

    猜你喜欢
    • 2013-02-28
    • 1970-01-01
    • 2023-03-09
    • 2014-02-14
    • 2013-03-07
    • 1970-01-01
    • 2015-04-29
    • 2019-07-12
    • 1970-01-01
    相关资源
    最近更新 更多