为什么Capybara认为我的URI方案格式无效? Addressable :: URI :: InvalidURIError:无效的方案格式:127.0.0.1

时间:2018-04-02 20:57:41

标签: ruby-on-rails rspec capybara

当我尝试运行此特定测试时,会出现此错误。当我尝试运行所有测试时,由于其他原因它失败了。如何在没有此错误的情况下运行此测试?它显然是一个有效的URL。

C:\Users\Chloe\workspace\catalyst_research>rspec ./spec/features/ctdbase_purchase_spec.rb:28
http://ctdbase.org/  # output of puts
  1) CTD Purchase Unregistered user visits the CTD product page directly and makes purchase
     Failure/Error: visit url

     Addressable::URI::InvalidURIError:
       Invalid scheme format: 127.0.0.1
     # ./spec/support/ctdbase_support.rb:8:in `visit_ctd'
     # ./spec/features/ctdbase_purchase_spec.rb:11:in `block (2 levels) in <top (required)>'

这是代码

ctdbase_support.rb
  def visit_ctd(path)
    url = "#{CTD_URL}#{path.starts_with?('/') ? '' : '/'}#{path}"
    puts url
    visit url # line 8
  end
ctdbase_purchase_spec.rb
  before do
    delete_downloads
    visit_ctd '/' # line 11

以下是完整的回溯:

C:\Users\Chloe\workspace\catalyst_research>rspec -b ./spec/features/ctdbase_purchase_spec.rb:28
...
  1) CTD Purchase Unregistered user visits the CTD product page directly and makes purchase
     Failure/Error: visit "#{CTD_URL}#{path.starts_with?('/') ? '' : '/'}#{path}"

     Addressable::URI::InvalidURIError:
       Invalid scheme format: 127.0.0.1
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/addressable-2.5.2/lib/addressable/uri.rb:874:in `scheme='
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/addressable-2.5.2/lib/addressable/uri.rb:799:in `block in initialize'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/addressable-2.5.2/lib/addressable/uri.rb:2355:in `defer_validation'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/addressable-2.5.2/lib/addressable/uri.rb:796:in `initialize'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/addressable-2.5.2/lib/addressable/uri.rb:136:in `new'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/addressable-2.5.2/lib/addressable/uri.rb:136:in `parse'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/addressable-2.5.2/lib/addressable/template.rb:413:in `match'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/request_pattern.rb:151:in `block in matches?'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/request_pattern.rb:151:in `any?'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/request_pattern.rb:151:in `matches?'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/request_pattern.rb:33:in `matches?'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/stub_registry.rb:58:in `block in request_stub_for'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/stub_registry.rb:57:in `each'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/stub_registry.rb:57:in `detect'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/stub_registry.rb:57:in `request_stub_for'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/stub_registry.rb:50:in `response_for_request'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/http_lib_adapters/net_http.rb:79:in `request'
     # C:/ruby24/lib/ruby/2.4.0/net/http.rb:1165:in `get'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/capybara-2.16.1/lib/capybara/server.rb:82:in `block in responsive?'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/http_lib_adapters/net_http.rb:123:in `start_without_connect'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/http_lib_adapters/net_http.rb:150:in `start'
     # C:/ruby24/lib/ruby/2.4.0/net/http.rb:608:in `start'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/capybara-2.16.1/lib/capybara/server.rb:82:in `responsive?'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/capybara-2.16.1/lib/capybara/server.rb:98:in `boot'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/capybara-2.16.1/lib/capybara/session.rb:88:in `initialize'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/capybara-2.16.1/lib/capybara.rb:304:in `new'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/capybara-2.16.1/lib/capybara.rb:304:in `current_session'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/capybara-2.16.1/lib/capybara/dsl.rb:45:in `page'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/capybara-2.16.1/lib/capybara/dsl.rb:50:in `block (2 levels) in <module:DSL>'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-rails-3.7.2/lib/rspec/rails/example/feature_example_group.rb:29:in `visit'
     # ./spec/support/ctdbase_support.rb:6:in `visit_ctd'
     # ./spec/features/ctdbase_purchase_spec.rb:11:in `block (2 levels) in <top (required)>'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:447:in `instance_exec'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:447:in `instance_exec'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:350:in `run'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:509:in `block in run_owned_hooks_for'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:508:in `each'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:508:in `run_owned_hooks_for'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:595:in `block in run_example_hooks_for'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:594:in `reverse_each'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:594:in `run_example_hooks_for'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:464:in `run'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:494:in `run_before_example'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:253:in `block in run'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:500:in `block in with_around_and_singleton_context_hooks'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:457:in `block in with_around_example_hooks'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:466:in `block in run'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:606:in `block in run_around_example_hooks_for'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:342:in `call'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-rails-3.7.2/lib/rspec/rails/adapters.rb:127:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:447:in `instance_exec'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:447:in `instance_exec'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:375:in `execute_with'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:608:in `block (2 levels) in run_around_example_hooks_for'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:342:in `call'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:609:in `run_around_example_hooks_for'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:466:in `run'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:457:in `with_around_example_hooks'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:500:in `with_around_and_singleton_context_hooks'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:251:in `run'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example_group.rb:628:in `block in run_examples'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example_group.rb:624:in `map'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example_group.rb:624:in `run_examples'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example_group.rb:590:in `run'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:118:in `block (3 levels) in run_specs'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:118:in `map'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:118:in `block (2 levels) in run_specs'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/configuration.rb:1896:in `with_suite_hooks'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:113:in `block in run_specs'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/reporter.rb:79:in `report'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:112:in `run_specs'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:87:in `run'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:71:in `run'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:45:in `invoke'
     # C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/exe/rspec:4:in `<top (required)>'
     # C:/ruby24/bin/rspec:23:in `load'
     # C:/ruby24/bin/rspec:23:in `<main>'

似乎这个项目正在使用WebMock。这是webmock.rb文件

spec/config/webmock.rb
require 'webmock/rspec'
WebMock.allow_net_connect!

GOOGLE_STUB_WITHOUT_IP = {
  "city" => "Mountain View",
  "region" => "California",
  "region_code" => "CA",
  "country" => "US",
  "country_name" => "United States",
  "continent_code" => "NA",
  "postal" => "94035",
  "latitude" => 37.386,
  "longitude" => -122.0838,
  "timezone" => "America/Los_Angeles",
  "utc_offset" => "-0700",
  "country_calling_code" => "+1",
  "currency" => "USD",
  "languages" => "en-US,es-US,haw,fr",
  "asn" => "AS15169",
  "org" => "Google LLC"
}

uri_template =
  Addressable::Template.new "https://ipapi.co/{ip_address}/json/"

WebMock.stub_request(:get, uri_template).to_return { |request|
  {
    # FIXME: This should dynamically return the ip address
    # body: GOOGLE_STUB_WITHOUT_IP.merge('ip' => request.ip_address)
    body: GOOGLE_STUB_WITHOUT_IP.merge('ip' => '8.8.8.8').to_json,
    headers: { 'Content-Type' => 'application/json' }
  }
}

1 个答案:

答案 0 :(得分:0)

您遇到了WebMock和Addressable - https://github.com/bblimke/webmock/issues/489https://github.com/bblimke/webmock/pull/739组合中的错误。问题是,WebMock最终会创建URL的变体以与模板进行比较,并在Capybara启动应用程序时最终执行类似127.0.0.1:<some_port>/__identify的操作。正确的解决方案是让WebMock修复其发送到Addressable的网址,但您可以通过设置Capybara.server_host='localhost'来暂时解决此问题,这会导致网址变化最终为localhost:<some_port>/__identify和Addressable将localhost视为可能的方案(可能有效)。它是一个黑客,但它可能会阻止你,直到WebMock得到修复。另一种选择是停止在WebMock中使用URL模板,而是使用正则表达式。