![](/img/trans.png)
[英]Cucumber not finding a route in test mode that I can see in development mode
[英]Cucumber not using a route in test mode that I can use in development mode
在开发模式下,我可以将浏览器指向http://arewesmallyet.dev/data,然后看到所有已抓取数据的json转储,表明路由映射和应用程序中的数据操作正在执行它们的工作。
在/features/step_definitions/landing_steps.rb中,我有:
When /^I visit the (.*) page$/ do |webpage|
visit path_to(webpage)
end
在/features/support/url.rb中,我有:
def path_to(webpage)
case webpage
when 'home'
'/'
when 'data'
'/data'
end
end
rake routes
给出:
Application: Arewesmallyet
URL REQUEST PATH
(:index) GET /
(:data) GET /data(.:format)
但是当我运行黄瓜时,我得到:
Scenario: data page # features/landing.feature:10
Given records exist in the database # features/step_definitions/landing_steps.rb:9
When I visit the data page # features/step_definitions/landing_steps.rb:1
File saved to ~/Developer/Ruby/arewesmallyet/capybara-timestamp.html.
Please install the launchy gem to open the file automatically.
Then I should be served the json data # features/step_definitions/landing_steps.rb:15
proper content missing (Minitest::Assertion)
features/landing.feature:13:in `Then I should be served the json data'
capybara-timestamp.html的内容来自“ /”。 当我puts path_to(webpage)
添加到步骤中时,我得到了正确的打印路径。 但是current_url
给出'/'。
实际上,如果我将步骤更改为:
When /^I visit the (.*) page$/ do |webpage|
puts path_to(webpage)
visit path_to(webpage)
puts 'first:'+current_path
end
(截断的)输出为:
When I visit the data page # features/step_definitions/landing_steps.rb:1
/data
first:/
我应该如何找到这个问题的原因?
我在使用byebug进行调试时发现了一些有趣的东西:
当我告诉Capybara访问“ /数据”,“ / data.json”,“ / data.js”,“ /”或任何其他路径时,它正在尝试访问http://www.example.com 。 由于所有路径都变成了http://www.example.com ,因此该网址没有路径组件,因此我的应用显然可以使用“ /”。 我不想访问远程URL,这就是为什么我使用'/'和'/ data'路径以及:rack_test
驱动程序的原因; 但根据https://groups.google.com/forum/#!topic/ruby-capybara/HMKCIDJAA6w上的讨论,水豚完全被打破了吗?
有任何解决方法,还是这枚宝石只是一文不值?
我在https://groups.google.com/forum/#!topic/ruby-capybara/SaB81spfil8上报告了此问题,我们将看看他们是否愿意进行修复。
您可以使用byebug进行调试,然后进入代码以查看发生了什么。 将byebug添加到Gemfile中的development,test组并进行捆绑安装。 然后,在进行访问之前,在步骤定义中添加一个byebug语句。
我还要在您的控制器中添加一条Byebug语句。 基本技术是找出问题,然后介入调查。 在这种情况下,您可能必须介入很多水豚和机架代码才能解决此问题。
话虽如此,我怀疑问题出在你的路线上。 我猜想,与您在测试时使用的驱动程序相比,浏览器在解释路由方面更加自由。 因此,我尝试将数据路由更改为“ data.json”或data.html,然后看看会发生什么。 如果这样不起作用,请将route.rb的内容添加到您的问题中
如果您未设置app_host,则Capybara会故意使用example.com,并且由于如果域不正确,我会重定向,因此这是不可接受的,因此我必须在我的features/support/env.rb
文件中设置app host,如下所示:
Capybara.app_host = 'http://arewesmallyet.dev'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.