简体   繁体   English

黄瓜未在测试模式下使用可以在开发模式下使用的路由

[英]Cucumber not using a route in test mode that I can use in development mode

In development mode, I can point my browser at http://arewesmallyet.dev/data and I see a json dump of all of my scraped data, indicating that the route mapping, and the data action in the application are doing their job. 在开发模式下,我可以将浏览器指向http://arewesmallyet.dev/data,然后看到所有已抓取数据的json转储,表明路由映射和应用程序中的数据操作正在执行它们的工作。

In /features/step_definitions/landing_steps.rb I have: 在/features/step_definitions/landing_steps.rb中,我有:

When /^I visit the (.*) page$/ do |webpage|
  visit path_to(webpage)
end

In /features/support/url.rb I have: 在/features/support/url.rb中,我有:

def path_to(webpage)
  case webpage
    when 'home'
      '/'
    when 'data'
      '/data'
  end
end

and rake routes gives: rake routes给出:

Application: Arewesmallyet
    URL         REQUEST  PATH
    (:index)      GET    /
    (:data)       GET    /data(.:format)

But when I run cucumber, I get: 但是当我运行黄瓜时,我得到:

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'

and capybara-timestamp.html has the content from '/'. capybara-timestamp.html的内容来自“ /”。 When I add puts path_to(webpage) to the step, I get the correct paths printed. 当我puts path_to(webpage)添加到步骤中时,我得到了正确的打印路径。 But current_url gives '/'. 但是current_url给出'/'。

Infact if I change the step to: 实际上,如果我将步骤更改为:

When /^I visit the (.*) page$/ do |webpage|
  puts path_to(webpage)
  visit path_to(webpage)
  puts 'first:'+current_path
end

the (truncated) output is: (截断的)输出为:

 When I visit the data page  # features/step_definitions/landing_steps.rb:1
  /data
  first:/

How should I go about finding the cause of this problem? 我应该如何找到这个问题的原因?

Something interesting I found while debugging with byebug: 我在使用byebug进行调试时发现了一些有趣的东西:

Capybara is trying to visit http://www.example.com when I tell it to visit '/data', '/data.json', '/data.js', '/', or any other path. 当我告诉Capybara访问“ /数据”,“ / data.json”,“ / data.js”,“ /”或任何其他路径时,它正在尝试访问http://www.example.com Since all paths get turned into http://www.example.com there is no path component to the url so my app obviously serves '/'. 由于所有路径都变成了http://www.example.com ,因此该网址没有路径组件,因此我的应用显然可以使用“ /”。 I do NOT want to access a remote URL, which is why I use the '/' and '/data' paths, and the :rack_test driver; 我不想访问远程URL,这就是为什么我使用'/'和'/ data'路径以及:rack_test驱动程序的原因; but based on the discussion at https://groups.google.com/forum/#!topic/ruby-capybara/HMKCIDJAA6w capybara is just completely broken? 但根据https://groups.google.com/forum/#!topic/ruby-capybara/HMKCIDJAA6w上的讨论,水豚完全被打破了吗?

Is there any workaround or is this gem just worthless? 有任何解决方法,还是这枚宝石只是一文不值?

I reported the issue at https://groups.google.com/forum/#!topic/ruby-capybara/SaB81spfil8 , we'll see if they bother to fix it. 我在https://groups.google.com/forum/#!topic/ruby-capybara/SaB81spfil8上报告了此问题,我们将看看他们是否愿意进行修复。

You can use byebug, to debug this, and step into the code to see what is happening. 您可以使用byebug进行调试,然后进入代码以查看发生了什么。 Add byebug to the development,test group in you Gemfile and do a bundle install. 将byebug添加到Gemfile中的development,test组并进行捆绑安装。 Then put a byebug statement in your step definition before you do the visit. 然后,在进行访问之前,在步骤定义中添加一个byebug语句。

I'd also but a byebug statement in your controller. 我还要在您的控制器中添加一条Byebug语句。 The basic technique is to isolate the problem, and then step in to investigate. 基本技术是找出问题,然后介入调查。 In this case you might have to step into quite alot of capybara and rack code to get to the problem. 在这种情况下,您可能必须介入很多水豚和机架代码才能解决此问题。

Having said that, I suspect that the problem is your route. 话虽如此,我怀疑问题出在你的路线上。 I'm guessing that a browser is much more liberal with how it inteprets routes, compared to the driver you are using when testing. 我猜想,与您在测试时使用的驱动程序相比,浏览器在解释路由方面更加自由。 So I would try changing the data route to 'data.json', or data.html, and see what happens. 因此,我尝试将数据路由更改为“ data.json”或data.html,然后看看会发生什么。 If that doesn't work, add the contents of routes.rb to your question 如果这样不起作用,请将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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM