views:

20

answers:

1

Hi I'm writing a spec and i need to setup a test route from inside the spec however when i use Rails::Application.routes.draw it overwrites all the other routes.

Is there a way to do this in rails 3?

Cheers

Dan

A: 

The general solution is to use some method to actually switch our the route set - namely, either using mocking or just making the scope that uses the routes to return a new ActionDispatch::Routing::RouteSet.new - this prevents you from using the default routes but in the scenario where you're testing a something that revolves around the routes, it works perfectly.

If, alternatively, you're testing code e.g. helpers that use a route, you'll either need to look at saving the state of the routes or something similar to prevent it from clearing them. When in doubt, I suggest looking at the ActionDispatch code and tests (e.g. http://github.com/rails/rails/blob/master/actionpack/test/dispatch/url_generation_test.rb)

Sutto