rspec: undefined method route_for error

This post was imported from blogger, to see the original, likely better-formatted post see


I’m now in the process of learning Ruby on Rails and its friends, like rspec. Firmly believing in TDD, I jumped on the rpsec bandwagon as soon as I could. The second thing I started to test was my routes. My test looked something like this:

describe "should create :page param for / url" do
route_for(:controller => 'pages', :action => 'index').should == "/"

But sadly this resulted in the following error:

pages_controller_routing_spec.rb:14: undefined method `route_for' for # (NoMethodError)

After some googling I found a thread on the rspec mailing list that revealed a few different causes:

  1. spec/controllers in their path (i.e. spec/controllers/foo_controller_spec)
  2. describe “route generation”, :type => :controller do

My spec class was a controller, so that wasn’t the issue. The second option is only required when the controller test is not in spec/controllers, so that wasn’t the issue. I had scoured the rspec docs, and everything I read said it should work.

I had missed the most obvious thing of all — I had said describe when I meant it:

it "should create :page param for / url" do
route_for(:controller => 'pages', :action => 'index').should == "/"

So, if you happen to encounter a similar error… don’t forget to check the obvious.

No comments yet.

Leave a Reply