Where to test routes in ruby on rails

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

Where to test routes in ruby on rails?

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

Addition:

To be e

相关标签:
5条回答
  • 2021-02-19 00:37

    According to a comment in this rails bug, the proper place is in a functional test. If you try to test them in an integration test, the routes 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'
      end
    end
    

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

    0 讨论(0)
  • 2021-02-19 00:42

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

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

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-19 00:43

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

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

    0 讨论(0)
  • 2021-02-19 00:56

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

    class HomeRoutesTest < ActionController::TestCase
    
      test "must route to home index" do
        assert_routing '/', controller: "home", action: "index"
      end
    
    end
    

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

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

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

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

    0 讨论(0)
提交回复
热议问题