[rspec-users] Possible improvements to routing spec API
win at wincent.com
Mon Jul 5 15:40:20 EDT 2010
El 05/07/2010, a las 20:18, Randy Harmon escribió:
> I'm uncertain about the need to easily specify one-directional routes.
> While in theory it sounds fine, I don't understand why you'd want to
> specify either a route that isn't recognized (why bother routing it, in
> this case?) or one that doesn't generate the given path with url_for()
> (does it generate some other path instead?).
Well, I find that most routes map in both directions (ie. basically anything that is specified using "resource" in the config/routes.rb file) but from time to time I find non-resource routes that map only one way (ie. are routable, but url_for won't necessarily generate what you want).
Mostly they seem to be routes declared with "match" like this one:
match '/posts/page/:page' => 'posts#index', :as => 'paginated_posts'
You can see here how the mapping doesn't work out the same in both directions:
- recognition: '/posts/page/2' is recognized as 'posts#index' with :page => '2'
- generation: 'posts#index' with :page => '2' generates '/posts?page=2'
Sometimes they can be reorganized so that they _do_ map in both directions; eg:
resources :posts do
get 'page/:page' => 'posts#index'
With that change, Rails does know how to do the reverse mapping.
But then there are ones which, as far as I know, can't be reorganized in that way; here's one example from my current app:
# the resource
# the shortcut to the links#show action
match 'l/:id' => 'links#show'
According to my experiments, the Rails routing assertions will say that URLs like 'l/foo' are routable, but won't generate in reverse. So there's a case for "assert_recognizes"/"map_to", for sure.
I agree with you that it doesn't make much sense to specify an unrecognizable route, but I guess it is conceivable that you could have routes that were recognizable in one direction, but only generated if fed a different set of parameters. So you'd need to test them using a pair of "map_from"/"map_to" or "assert_generates"/"assert_recognizes".
More information about the rspec-users