Search code examples
routescucumbercapybarapadrino

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.


Solution

  • Capybara deliberately uses example.com if you don't set app_host, and since I redirect if the domain is incorrect, that's unacceptable so I have to set app host in my features/support/env.rb file like so:

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