How do i use named routes in an Rails engine's specs?

545 views Asked by At

I have a Rails Engine with the engine.rb defined:

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

I have some routes defined:

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

Now, everything works great inside my dummy app. i can use the variable keywords.new_keyword_group_path to get a url to my path.

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 get the following error:

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

If i instead try to leave off the "keywords" prefix, and do:

visit new_keyword_group_path

i get 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?

1

There are 1 answers

0
user210881 On

i found a solution for this based on the Spree gem. creating a helper method for the prefix to grab the path from the engines routes like so:

def keywords
  Keywords::Engine.routes.url_helpers
end

then keywords.new_keyword_path works like a charm