将 webmock 与黄瓜一起使用

发布于 2024-11-09 06:30:13 字数 526 浏览 1 评论 0原文

我正在使用 webmock,但它不适用于黄瓜测试

在我的 Gemfile

  gem 'vcr'
  gem 'webmock'

和我的 features/support.env.rb 中,

require 'webmock/cucumber'
WebMock.allow_net_connect!

当我运行黄瓜测试时,我收到此错误。

    Real HTTP connections are disabled. Unregistered request:
 GET http://127.0.0.1:9887/__identify__ with headers
 {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}

我做错了什么或者缺少什么吗?

I am using webmock and it is not working for cucumber tests

In my Gemfile

  gem 'vcr'
  gem 'webmock'

And in my features/support.env.rb, I have

require 'webmock/cucumber'
WebMock.allow_net_connect!

When I run my cucumber tests I am getting this error.

    Real HTTP connections are disabled. Unregistered request:
 GET http://127.0.0.1:9887/__identify__ with headers
 {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}

Am I doing anything wrong or is sth missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半夏半凉 2024-11-16 06:30:14

扩展迈伦·马斯顿的答案。如果您需要为其他东西保留 localhost,例如您可能希望 VCR 捕获请求的机架应用程序,则需要创建一个自定义匹配器,而不是忽略所有 localhost 请求。

require 'vcr'

VCR.configure do |c|
  c.hook_into :webmock
  c.ignore_localhost = false

  c.ignore_request do |request|
    localhost_has_identify?(request)
  end
end


private
def localhost_has_identify?(request)
  if(request.uri =~ /127.0.0.1:\d{5}\/__identify__/)
    true
  else
    false
  end
end

Expanding on Myron Marston's answer. If you need to keep localhost for something else, such as a Rack App, that you might want VCR to capture request for, you will need to create a custom matcher rather than ignore all localhost requests.

require 'vcr'

VCR.configure do |c|
  c.hook_into :webmock
  c.ignore_localhost = false

  c.ignore_request do |request|
    localhost_has_identify?(request)
  end
end


private
def localhost_has_identify?(request)
  if(request.uri =~ /127.0.0.1:\d{5}\/__identify__/)
    true
  else
    false
  end
end
陌路黄昏 2024-11-16 06:30:14

如果您同时使用 RSpec 和 Cucumber,则可能需要为 WebMock 创建两个配置文件(与 VCR 一起使用时):

# spec/support/webmock.rb
# Config for RSpec
require 'webmock/rspec'
WebMock.disable_net_connect!(allow_localhost: true)

# features/support/webmock.rb
# Config for Cucumber
require 'webmock/cucumber'
WebMock.disable_net_connect!(allow_localhost: true)

在此处记录此文件,以便人们在谷歌搜索 __identify__ 时找到。错误看起来像...

Real HTTP connections are disabled.
Unregistered request: GET http://127.0.0.1:59005/__identify__ 

If you use both RSpec and Cucumber, you might need to create two config files for WebMock (when using with VCR):

# spec/support/webmock.rb
# Config for RSpec
require 'webmock/rspec'
WebMock.disable_net_connect!(allow_localhost: true)

# features/support/webmock.rb
# Config for Cucumber
require 'webmock/cucumber'
WebMock.disable_net_connect!(allow_localhost: true)

Documenting this here for people to find when googling for __identify__. Errors look like...

Real HTTP connections are disabled.
Unregistered request: GET http://127.0.0.1:59005/__identify__ 
屋顶上的小猫咪 2024-11-16 06:30:13

首先,如果您使用的是 VCR,则无需使用 require 'webmock/cucumber' 行和 WebMock.allow_net_connect! 行配置 webmock。 VCR 会为您处理任何必要的 WebMock 配置。

触发错误的请求看起来像是来自 Capybara。当您使用其中一个 JavaScript 驱动程序时,水豚会使用简单的机架服务器启动您的应用程序,然后轮询特殊的 __identify__ 路径,以便它知道何时完成启动。

VCR 支持忽略本地主机请求,这样就不会干扰此操作。 relish 文档有完整的故事但简短的版本是您需要添加 VCR 配置,如下所示:

VCR.config do |c|
  c.ignore_localhost = true
end

First off, if you're using VCR, you don't need to configure webmock with the require 'webmock/cucumber' line and the WebMock.allow_net_connect! line. VCR takes care of any necessary WebMock configuration for you.

The request that is triggering the error looks like it's coming from Capybara. When you use one of the javascript drivers, capybara boots your app using a simple rack server, and then polls the special __identify__ path so it knows when it has finished booting.

VCR includes support for ignoring localhost requests so that it won't interfere with this. The relish docs have the full story but the short version is that you need to add VCR configuration like this:

VCR.config do |c|
  c.ignore_localhost = true
end
往事随风而去 2024-11-16 06:30:13

我有同样的错误,但不使用录像机。我可以通过添加以下内容来解决此问题:

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

到我的 env.rb 文件中。

I had the same error though do not use VCR. I was able to resolve this by adding:

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

to my env.rb file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文