This project is archived and is in readonly mode.

#5922 open
Andrea Campi

Functional tests cannot add a route

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

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?

Comments and changes to this ticket

  • Santiago Pastorino

    Santiago Pastorino February 5th, 2011 @ 08:56 PM

    • 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.

  • Santiago Pastorino

    Santiago Pastorino February 5th, 2011 @ 08:56 PM

    • State changed from “open” to “stale”
  • Murray Steele

    Murray Steele February 17th, 2011 @ 06:19 PM

    • 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.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages