I have this route (Rails 3) which handles redirecting shortened urls:
handles the shortened url lookup
match ‘:hash’ => redirect { |params|
Sharing.get_url_path_by_short_url(params[:hash]) }, :constraints => {
:hash
=> /[a-zA-Z0-9]{7}/ }
It works correctly at the ui. However am having trouble getting a spec
working for it. Note that in the UI the user must be logged in to get to
the
url, but the traffic first hits the directed route and then app
controller
handles if the user is not logged in.
In spec/routing/routing_spec.rb (note that all necessary test data for
it to
pass is set up and verified):
describe “Shorter URL redirect” do
it “routes GET ‘STU1VWX’ to stations/1” do
{ :get => “/”, :hash => “STU1VWX” }.should route_to(
:controller => “stations”,
:action => “show”,
:params => {:id => 1} )
end
end
The problem is that I get this error:
Failure/Error: { :get => "/", :hash => "STU1VWX" }.should route_to(
The recognized options <{"controller"=>"sessions",
“action”=>“new”}>
did not match <{“controller”=>“stations”, “action”=>“show”,
“params”=>{:id=>1}}>, difference: <{“controller”=>“stations”,
“action”=>“show”, “params”=>{:id=>1}}>.
sessions#new is our root url. So I see that by calling :get => ‘/’ the
:hash
seems to be ignored. I have confirmed this by changing the root url and
seen
the new root path show in the error above.
Note that if I change the test to:
{ :get => "STU1VWX" }.should route_to( ...
or
{ :get => "/STU1VWX" }.should route_to( ...
I get a ‘no route matches’ exception
I have a few conjectures as to that is happening:
- The dynamic route is ignored (i.e. the :hash parameter is being
ignored). Calling in debugger
“Rails.application.routes.recognize_path(‘STU1VWX’)” returns a no
route
matches exception. - The way routes is being tested that the proc on the route does not
get
run eventhough the route is seen - I need to handle authentication (this would confuse me a bit as it
would seem that on a routes test we should just be testing the output
of the
router.
If anyone can give me some ideas I would appreciate it.
Best,
David