简体   繁体   中英

How do I test named routes?

I get the following error when testing a named route

1) Failure:
test_settings_route(ProjectsControllerTest) [/test/functional/projects_controller_test.rb:15]:
The generated path <"/projects/1/edit"> did not match <"/projects/1/settings">

Here's the test and what I put in my routes file

# projects_controller_test.rb
require 'test_helper'
class ProjectsControllerTest < ActionController::TestCase
def test_settings_route
  assert_routing '/projects/1/settings', :controller => 'projects', :action => 'edit', :id => '1'
end

# routes.rb
map.settings '/projects/:id/settings', :controller => 'projects', :action => 'edit'

With that entry in my routes file my application behaves as expected - which is to render the project's edit template when /projects/1/settings is requested - but I just don't understand how to get my test green.

I've also tried the shoulda macro with the same result

should_route :get, "/projects/1/settings", :controller => 'projects', :action => 'edit', :id => '1'

I had map.resources :projects above my named route in routes.rb. Moving my named route to the top fixed my test.

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