黄瓜和水豚,如何打开外部网址或访问外部网址

Bil*_*rat 18 ruby ruby-on-rails cucumber capybara ruby-on-rails-3

我正在使用黄瓜和水豚.在rails 3.0.9平台上.我得到这个测试用例失败:日志是:

(::) failed steps (::)

No route matches "/wiki/Baltimore_Ravens" (ActionController::RoutingError)
<internal:prelude>:10:in `synchronize'
./features/step_definitions/web_steps.rb:20:in `/^(?:|I )am on (.+)$/'
features/annotate.feature:7:in `Given I am on a web page'

Failing Scenarios:
cucumber features/annotate.feature:11 # Scenario: launch annotation/ logged in

6 scenarios (1 failed, 5 skipped)
63 steps (1 failed, 62 skipped)
Run Code Online (Sandbox Code Playgroud)

文件web_steps:得到这段代码:

19 Given /^(?:|I )am on (.+)$/ do |page_name|
20   visit path_to(page_name)
21 end
Run Code Online (Sandbox Code Playgroud)

文件annotate.feature得到了这段代码:

7 Given I am on a web page
Run Code Online (Sandbox Code Playgroud)

"web页面"在support/paths.rb中定义为:

when /a web page/
  'http://en.wikipedia.org/wiki/Baltimore_Ravens'
Run Code Online (Sandbox Code Playgroud)

显然这是一个外部网址.我想打开它,水豚和黄瓜不允许我这样做.所以,帮我找一个在黄瓜测试案例中打开外网的方法!

Mar*_*oda 22

Capybara使用RackTest作为默认驱动程序,此驱动程序不允许访问外部URL(即测试远程应用程序).

如果您想访问外部网址(例如,测试您的应用程序正确重定向),您基本上有两个选项:

1 /使用其他驱动程序,例如硒:

before do
  Capybara.current_driver = :selenium
end
Run Code Online (Sandbox Code Playgroud)

然后,在代码中,您可以像这样调用url:

visit 'http://en.wikipedia.org/wiki/Baltimore_Ravens'
Run Code Online (Sandbox Code Playgroud)

或者,如果您设置默认的app_host,如下所示:

Capybara.app_host = 'http://en.wikipedia.org'
Capybara.run_server = false # don't start Rack
Run Code Online (Sandbox Code Playgroud)

然后你可以调用网址:

visit '/wiki/Baltimore_Ravens'
Run Code Online (Sandbox Code Playgroud)

您可以在spec_helper.rb中配置驱动程序和应用程序主机,以便在所有规范中全局启用它们:

Capybara.configure do |config|
  config.current_driver = :selenium
  config.run_server = false
  config.app_host   = 'http://en.wikipedia.org'
end
Run Code Online (Sandbox Code Playgroud)

2 /使用capybara-mechanize