user2490003
user2490003

Reputation: 11900

RSpec feature specs can't find routes for Rails Engine

I'm developing a Rails Engine using rails v5.1.0 and rspec-rails 3.5.2.

I have a simple feature spec:

require "rails_helper"

module MyEngineName
  RSpec.feature "Some Feature", type: :feature do
    it "user can navigate to page and blah blah", :js do
      visit edit_job_path(1)
      # .... other stuff
    end
  end
end

This raises the error

undefined method `edit_job_path' for #<RSpec::ExampleGroups::SomeFeature:0x007fc098a570e8>

because the route helper edit_job_path can not be found.

Is there something special I need to do in order to allow my feature specs to access my engine routes?

The RSpec documentation mentions that you can specify the engine routes, but that only appears to be for routing specs. When I added it to the feature specs, it fails with undefined method 'routes'

Thanks!

EDIT: Since my routes file was requested, adding it here. It's pretty short -

# config/routes.rb
MyEngineName::Engine.routes.draw do
  root to: redirect("/my_engine_name/jobs")
  resources :jobs
end

List of all routes from rake

> rake app:routes
   ....
   ....

Routes for MyEngineName::Engine:
    root GET    /                        redirect(301, /my_engine_name/jobs)
    jobs GET    /jobs(.:format)          my_engine_name/jobs#index
         POST   /jobs(.:format)          my_engine_name/jobs#create
 new_job GET    /jobs/new(.:format)      my_engine_name/jobs#new
edit_job GET    /jobs/:id/edit(.:format) my_engine_name/jobs#edit
     job GET    /jobs/:id(.:format)      my_engine_name/jobs#show
         PATCH  /jobs/:id(.:format)      my_engine_name/jobs#update
         PUT    /jobs/:id(.:format)      my_engine_name/jobs#update
         DELETE /jobs/:id(.:format)      my_engine_name/jobs#destroy

Upvotes: 7

Views: 2312

Answers (2)

Jaswinder
Jaswinder

Reputation: 1535

For API request specs, I tried both ways but it doesn't work

routes { MyEngine::Engine.routes }

and

RSpec.configure do |config|
  config.before :each, type: :request do
    helper.class.include MyEngine::Engine.routes.url_helpers
  end
end

If the above solutions doesn't work, try loading helper urls as rspec configs as below:

RSpec.configure do |config|
  config.include MyEngine::Engine.routes.url_helpers
end

Upvotes: 3

chumakoff
chumakoff

Reputation: 7044

Something from this should help. Make sure that you have MyEngineName::Engine.routes, not MyEngineName.routes

require "rails_helper"

module MyEngineName
  RSpec.feature "Some Feature", type: :feature do
    routes { MyEngineName::Engine.routes }

    it "user can navigate to page and blah blah", :js do
    # ...

or (another solution)

# place this in `spec/rails_helper.rb` file
RSpec.configure do |config|
  config.before :each, type: :feature do
    helper.class.include MyEngineName::Engine.routes.url_helpers
  end
end

Upvotes: 2

Related Questions