Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Functional tests cannot add a route

#5922

I may be overlooking something, but:

I have a functional test that uses its own mock controller, which prepares some data and ends up calling a helper.
Among other things, I want to test that this helper generates the correct URLs, so I need my routes from config/routes.rb.
To be able to invoke actions on my mock controller, I have:

  setup do
    Rails.application.routes.draw do
      match '/foo' => "tags_tree_test/tags_tree_test#index"
    end
  end

  teardown do
    Rails.application.reload_routes!
  end

The problem is that #draw first clears existing routes.
Of course this would just work if I used an existing controller for which I already have a route in config/routes.rb--but I want to run this test in a clean environment.

Am I correct that there's no way to do this?
Do we want to support this use case?

Reported by Andrea Campi · November 5th, 2010 @ 05:03 PM

State: open
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 5th, 2011 @ 08:56 PM

  2. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 5th, 2011 @ 08:56 PM

  3. Murray Steele
    Murray Steele
    • State changed from stale to open

    [state:open]

    It's certainly still a problem as routes are cleared if we redraw them in our tests and sometimes we want the new route and our application routes. Not sure what the solution is, but I got around it as follows:

      def setup
        routes = Rails.application.routes
        routes.disable_clear_and_finalize = true
        routes.clear!
        Rails.application.routes_reloader.paths.each { |path| load(path) }
        routes.draw do
          match "/test-action" => "my_controller_test/test#index"
        end
        routes.disable_clear_and_finalize = false
        routes.finalize!
      end
    
      def teardown
        Rails.application.reload_routes!
      end
    

    But this is clearly upsetting. Perhaps the with_routing helper from ActionDispatch::Assertions::RoutingAssertions could be updated to work in 2 ways. The default is to work as it does now, where the routes defined in the block are the only available ones. The 2nd mode (or new helper) would provide routing that keeps the existing app routes around and adds those in the block to them. Both modes should reset back to the default app routes at the end of the block.

    February 17th, 2011 @ 06:19 PM