Where to test routes in ruby on rails

前端 未结 5 1864
耶瑟儿~
耶瑟儿~ 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: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.

提交回复
热议问题