[ACCEPTED]-Where to test routes in ruby on rails-routing

Accepted answer
Score: 18

Routes should be done as part of integration 19 tests. Integration tests are where you test 18 the important work flows of your application 17 - more specifically whether a URL is defined 16 or not seems to be an important workflow.

Your 15 integration test would look like any normal integration test:

# /tests/integration/routes_test.rb
require 'test_helper'

class RoutesTest < ActionController::IntegrationTest
  test "route test" do
    assert_generates "/photos/1", { :controller => "photos", :action => "show", :id => "1" }
    assert_generates "/about", :controller => "pages", :action => "about"

As 14 to @jemminger's response of not testing routes - While 13 it is Rail's tests that verify that routes.rb 12 works, it's not Rail's responsibility to 11 test whether http://yoursite.com/users is defined in your routes. The 10 caveat is that most route testing could 9 be done in existing integration tests, so 8 specific tests for routes could be redundant.

The 7 specific use case I can think of are all 6 the people that have already, or are going 5 to upgrade from Rails 2 to Rails 3. The 4 code to define routes has changed significantly, and 3 it's better to find out from tests that 2 the routes were upgraded correctly, than 1 from users when they report 404 errors.

Score: 10

Why do you feel the need to test the routes? Purely 9 to make sure that the routes defined in 8 your routes.rb actually work? If so, then 7 don't. That's not the job of your application's 6 tests to make sure that the framework's 5 internals operate properly - that's the 4 job of the Rails framework's own tests.

If 3 perhaps you have some sort of dynamic/user 2 definable route that you want to test, I'd 1 probably go with integration.

Score: 10

I suggest you create a test file for the 18 routes in your test/controllers folder.

class HomeRoutesTest < ActionController::TestCase

  test "must route to home index" do
    assert_routing '/', controller: "home", action: "index"


It was mentioned 17 that they belong to integration test. I 16 disagree. You merely test routes, that's 15 it. So it would rather fall into functional 14 or even unit testing instead of integration 13 testing.

You can find a reference in the 12 Rails RailsGuide Testing, section 9. Testing Routes

Integration tests are for the flow, where 11 you test the interaction of different controller 10 action, e.g. executing a business process 9 like user logs in, browses the site and 8 puts an item into the basket. That said 7 your integration tests won't work if your 6 routes don't work. Thus many say that integration 5 tests is the place where you test routes. However, considering 4 the development cycle, you would create 3 unit tests, controller tests, route tests, etc. first 2 before doing the integration tests.

And on 1 another note: assert_routing does both tests: assert_generates and assert_recognizes.

Score: 2

According to a comment in this rails bug, the proper place 10 is in a functional test. If you try to test 9 them in an integration test, the routes 8 you established in routes.rb will not be available.

require 'test_helper'

class FooControllerTest < ActionController::TestCase
  test "foo routes" do
    assert_recognizes({:controller => 'foo_controller', :action => 'list'}, '/foos'

Route 7 tests are good places to list links that 6 exist in the wild, and avoid inadvertently 5 breaking them with a code change. It's a 4 little strange that the tests are scoped 3 to a controller, since it's the route set 2 as a whole you're actually testing, but 1 I haven't heard of a 'route set test'.

Score: 0

If you are using rspec, then the most natural 3 place for routing tests is in the directory 2 spec/routing. From The Rspec documentation:

Routing specs are marked by :type => :routing or 1 if you have set config.infer_spec_type_from_file_location! by placing them in spec/routing.

More Related questions