Capybara doesn't find my route in engine

900 Views Asked by At

I started a new Rails 5 applications for myself to see how I can modularise an application with engines and simple test purposes. I decided to use Rspec and Capybara for integration tests.

I have a very basic feature rspec:

okinsmen/components/okm_backend/spec/features/humen_spec.rb

require 'rails_helper'

feature "Humen process" do
  scenario "go to index page" do
    visit '/humen'
    expect(page).to have_content("List of humen")
  end
end

That's my route:

okinsmen/components/okm_backend/config/routes.rb

    OkmBackend::Engine.routes.draw do
      root 'dashboards#index'
      resource :dashboards, only: [:index]
      resources :humen
    end

And the result of the command rails app:routes in the terminal:

Prefix Verb URI Pattern  Controller#Action
okm_backend      /okm_backend OkmBackend::Engine

Routes for OkmBackend::Engine:
      root GET    /                         okm_backend/dashboards#index
     humen GET    /humen(.:format)          okm_backend/humen#index
           POST   /humen(.:format)          okm_backend/humen#create  new_human GET    /humen/new(.:format)      okm_backend/humen#new edit_human GET    /humen/:id/edit(.:format) okm_backend/humen#edit
     human GET    /humen/:id(.:format)      okm_backend/humen#show
           PATCH  /humen/:id(.:format)      okm_backend/humen#update
           PUT    /humen/:id(.:format)      okm_backend/humen#update
           DELETE /humen/:id(.:format)      okm_backend/humen#destroy

All looks good, but I get always this error:

1) Humen process go to index page
     Failure/Error: visit '/humen'

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

If I replace visit "/humen" by humen_path, I get this error:

  1) Humen process go to index page
     Failure/Error: visit humen_path

     NameError:
       undefined local variable or method `humen_path' for #<RSpec::ExampleGroups::HumenProcess:0x00000006843ea0>

To solve this last issue, I have to add this line in my rails_helper.spec:

config.include OkmBackend::Engine.routes.url_helpers

Now the test pass.

But I can't get the test working with a string URL.

The application can be looked at https://github.com/patdec/okinsmen/tree/engines

It's very basic. Thanks if you can help me.

Update:

If I write my spec like this (as anwered by Tom Walpole):

scenario "go to index page" do
    visit '/okm_backend/humen'
    expect(page).to have_content("Humen list")
  end

it works

But this not:

scenario "display new page" do
    click_on '/okm_backend/humen/new'
    expect(page).to have_content("New human")
  end
3

There are 3 best solutions below

4
Thomas Walpole On BEST ANSWER

In your spec/dummy/config/routes.rb you're mounting the engine at /okm_backend for test purposes

Rails.application.routes.draw do
  mount OkmBackend::Engine => "/okm_backend"
end

That means to call visit with a string would need to be

visit '/okm_backend/humen'
3
Chaitanya Kale On

Have you tried doing app.humen_path in your rails console? You have to use that string as the URL.

0
a.barbieri On

A better solution is to use engine_name.some_path, which in your case is:

okm_backend.humen_path
# returns /okm_backend/humen