Skip to content

Instantly share code, notes, and snippets.

@stevenharman
Created May 18, 2011 04:00
Show Gist options
  • Save stevenharman/977969 to your computer and use it in GitHub Desktop.
Save stevenharman/977969 to your computer and use it in GitHub Desktop.
rspec-rails routing specs -- WTF?
# config/routes.rb
Blah::Application.routes.draw do
devise_for :users
get "/:username" => "users#show", :as => :user_profile
root :to => 'pages#home'
end
# > rake routes produces...
#
# new_user_session GET /users/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
# user_session POST /users/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
# destroy_user_session GET /users/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
# user_password POST /users/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
# new_user_password GET /users/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
# edit_user_password GET /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
# PUT /users/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
# cancel_user_registration GET /users/cancel(.:format) {:action=>"cancel", :controller=>"devise/registrations"}
# user_registration POST /users(.:format) {:action=>"create", :controller=>"devise/registrations"}
# new_user_registration GET /users/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
# edit_user_registration GET /users/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
# PUT /users(.:format) {:action=>"update", :controller=>"devise/registrations"}
# DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
# user_profile GET /:username(.:format) {:controller=>"users", :action=>"show"}
# root /(.:format) {:controller=>"pages", :action=>"home"}
# spec/routing/user_routing_spec.rb
require "spec_helper"
describe "routes for Users" do
# passes!
it "routes / to home" do
get("/").should route_to("pages#home")
end
# passes!
it "routes /:username to the users controller" do
get("/vader").should be_routable
end
it "routes /:username to the users controller" do
get("/vader").should route_to("users#show") # fails w/"Expected block to return value."
get("/vader").should route_to("users#show", :username => 'vader') # passes!
end
it "named route" do
# fails w/'No route matches {:controller=>"users", :action=>"show"}'
{ :get => user_profile_path }.should route_to(:controller => "users", :action => "show")
{ :get => user_profile_path("vader") }.should
route_to("users#show", :username => 'vader') # passes!
end
end
@dchelimsky
Copy link

The example on line 23 is telling you exactly why it's failing: there is no user_profile method, it's user_profile_path.

I think the one on line 18 should be:

get("/vader").should route_to("users#show", :username => "vader")

Not sure why the failure message is what it is, but let's see if this ^^ works first.

@stevenharman
Copy link
Author

@dchelimsky you are correct, line 18 needed the :username => "vader". And yes, it would be nice if the error message were a bit more precise.

re: user_profile_path - gah, of course! actually calling the correct path helper is a good idea :) mea culpa!

however, it then fails because No route matches {:controller=>"users", :action=>"show"}

@stevenharman
Copy link
Author

Thanks @aaronjensen and @joefiorini for pointing out that user_profile_path requires a parameter (the :username).

Clearly I'm too tired to be coding right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment