How do I use named routes in the Rails spec?

I have a Rails Engine with engine.rb installed:

module Keywords
  class Engine < ::Rails::Engine
    isolate_namespace Keywords
end

      

I have certain routes:

Keywords::Engine.routes.draw do
  resources :keyword_groups,  only: [:new] 
end

      

Now everything works fine inside my dummy app. i can use a variable keywords.new_keyword_group_path

to get my path url.

However, when I try to do the same in my rspec tests using capybara, it fails! I tried to add the following line to my spec_helper.rb as suggested:

config.include Keywords::Engine.routes.url_helpers

      

But, when I try to use the following line in my tests:

visit keywords.new_keyword_group_path

      

I am getting the following error:

NameError: undefined local variable or method `keywords' for #<RSpec::Core::ExampleGroup::Nested_1:0x00000108833390>

      

If I instead try to leave the "keywords" prefix and do:

visit new_keyword_group_path

      

I am getting this error:

ActionController::RoutingError: No route matches [GET] "/keyword_groups/new"

      

which makes sense as the path should be "/ keywords / keyword_groups / new".

How can I get the included URL helpers to use the correct namespace prefix?

+3


source to share


1 answer


I found a solution for this based on the Spree gem. by creating a prefix helper method to grab the path from the engine routes like this:

def keywords
  Keywords::Engine.routes.url_helpers
end

      



then keywords.new_keyword_path

works like a charm

+1


source







All Articles