Custom Routes With Shoulda

Friday, February 19, 2010

Custom Routes with Shoulda

posted at: February 19th, 2010

I’ve recently been using Shoulda as my testing framework of choice to speed up my Rspec test writing. One common pattern kept recurring which was totally vexing: I wasn’t able to test my custom routes. Just recently, I was writing a reviews controller that is mostly controlled by an admin user of the site. It’s logical to route the index request of the reviews controller to /admin.

I kept trying to force the request with the following failing code:

#don't use this, it's bad
#I want to reroute /reviews to /admin
get "/admin"

That was clearly wrong, but nothing immediately jumped out at me as to how to fix it. After much googling around, I finally found the quick and easy solution:

ruby code on github here

describe "/admin" do
    before(:each){
        get :index
    }

    it { should route(:get, "/admin").to(:controller =>; :reviews, :action =>; :index) }
    it { should respond_with(:success) }
end

Hope this saves someone else some time.

I didn't want to build an entire commenting engine. I'm lazy :) Tweet a comment using the hashtag #bamcrws and I'll reply. Those tweets will show up here.