简体   繁体   中英

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.

In /features/step_definitions/landing_steps.rb I have:

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

In /features/support/url.rb I have:

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

and rake routes gives:

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 '/'. When I add puts path_to(webpage) to the step, I get the correct paths printed. But current_url gives '/'.

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:

Capybara is trying to visit http://www.example.com when I tell it to visit '/data', '/data.json', '/data.js', '/', or any other path. Since all paths get turned into http://www.example.com there is no path component to the url so my app obviously serves '/'. I do NOT want to access a remote URL, which is why I use the '/' and '/data' paths, and the :rack_test driver; but based on the discussion at https://groups.google.com/forum/#!topic/ruby-capybara/HMKCIDJAA6w capybara is just completely broken?

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.

You can use byebug, to debug this, and step into the code to see what is happening. Add byebug to the development,test group in you Gemfile and do a bundle install. Then put a byebug statement in your step definition before you do the visit.

I'd also but a byebug statement in your controller. 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. If that doesn't work, add the contents of routes.rb to your question

如果您未设置app_host,则Capybara会故意使用example.com,并且由于如果域不正确,我会重定向,因此这是不可接受的,因此我必须在我的features/support/env.rb文件中设置app host,如下所示:

Capybara.app_host = 'http://arewesmallyet.dev'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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