开发者

How do I add a route from within a Rails App

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 r开发者_开发知识库outes.

Is there a way to do this in rails 3?

Cheers

Dan


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜