Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec feature specs can't find routes for Rails Engine

Tags:

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
like image 680
user2490003 Avatar asked Jun 15 '17 15:06

user2490003


1 Answers

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
like image 132
Jaswinder Avatar answered Oct 11 '22 12:10

Jaswinder