Ruby on Rails App Testing with Rspec and Capybara

2019-09-14 15:01发布

问题:

when i create a feature test for my application get the following error:

 ActionController::RoutingError:
   No route matches [GET] "/example"

My application uses subdomains and sub-applications(engines/modules) within these subdomains. Now when i set for Capybara the app_host or default_host through an feature_subdomain_helper like

Capybara.app_host = "example.lvh.me" or
Capybara.default_host = "example.lvh.me"

and into my rails_helper.rb i add the following code line

  config.extend SubdomainHelpers, type: :feature

I get the same error. Now i think the configured subdomain are not considered by my feature test.

My Rspec Version is: 3.2 and Capybara Version is: 2.4.4

My sample feature test looks like:

require 'rails_helper'
feature 'Example Page' do

  scenario 'visit example page' do

    visit "/example"

    expect(page).to have_content 'Example'
  end
end

Have someone an idea what i do wrong?

Edit: Mainapp routes:

constraints(Subdomain) do
  mount Example::Engine => '/', as: 'example'
end

Engine routes:

Example::Engine.routes.draw do

  scope '/example', nav_scope: 'example' do
  end
end

回答1:

The names of Capybara.default_host and Capybara.app_host are slightly misleading since they both need to be set as URLs to function properly

Capybara.default_host = "http://example.lvh.me"

If that doesn't fix your issue check rake routes and make sure the action you think is mounted at '/example' really is.