简体   繁体   中英

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM