Where to test routes in ruby on rails

前端 未结 5 1852
耶瑟儿~
耶瑟儿~ 2021-02-19 00:25

Where to test routes in ruby on rails?

  • unit tests?
  • functional tests?
  • integration tests?

Addition:

To be e

5条回答
  •  -上瘾入骨i
    2021-02-19 00:43

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

    Your 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"
      end
    end
    

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

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

提交回复
热议问题