Testing route helper methods and formatted routes
Saturday, February 7th, 2009Here’s a quick tip on how to test those helpers such as ‘new_post_path’ and ‘home_path’. Also those formatted routes such as ‘formatted_posts_path’. And why is important to test those.
Here’s how I test route helper methods.
# spec/controllers/ideas_controller_spec.rb
describe IdeasController do
describe "GET /ideas/new" do
it "should have helper method new_idea_path" do
get :new
new_idea_path.should_not be_nil
new_idea_path.should == '/ideas/new'
end
it "should have helper method formatted_new_idea_path" do
get :new, :format => :json
formatted_new_idea_path.should_not be_nil
formatted_new_idea_path.should == '/ideas/new.json'
end
(...)
end
end
And why it’s important to test those methods, since they’re already tested by Rails tests?
Rails 2.3 has removed all those formatted_xxx named routes. Turns out these routes ate up a lot of memory and served minimal purpose. Read article here.
So if you use something like formatted_new_user, and you need to upgrade the application to Rails 2.3, it would be broken without test coverage.