[rspec-users] Testing route with proc --- possible and how?

David Kahn dk at structuralartistry.com
Thu Mar 17 15:54:02 EDT 2011

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} )

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


    { :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

If anyone can give me some ideas I would appreciate it.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110317/724a5bb0/attachment.html>

More information about the rspec-users mailing list