Testing Routes using Rspec

Most Rails developers don’t test their routes separately, they write controller test and try to catch every possible scenarios in a controller test. Today I learned how to test route separately. I have created seven routes using

  resources :posts

Here are lists of routes thats been created by above code.

  rake routes

     root        /                         posts#index
    posts GET    /posts(.:format)          posts#index
          POST   /posts(.:format)          posts#create
 new_post GET    /posts/new(.:format)      posts#new
edit_post GET    /posts/:id/edit(.:format) posts#edit
     post GET    /posts/:id(.:format)      posts#show
          PUT    /posts/:id(.:format)      posts#update
          DELETE /posts/:id(.:format)      posts#destroy

Now below I have written tests for each routes


require 'spec_helper'

describe "Routes" do
  describe "PostsController" do
    it "routes get index" do
      expect(:get => "posts").to route_to(
        :controller => "posts",
        :action => "index"
      )
    end

    it "routes get new" do
      expect(:get => "posts/new").to route_to(
        :controller => "posts",
        :action => "new"
      )
    end

    it "routes get show" do
      expect(:get => "posts/1").to route_to(
        :controller => "posts",
        :action => "show",
        :id => "1"
      )
    end

    it "routes post create" do
      expect(:post => "posts").to route_to(
        :controller => "posts",
        :action => "create"
      )
    end

    it "routes get edit" do
      expect(:get => "posts/1/edit").to route_to(
        :controller => "posts",
        :action => "edit",
        :id => "1"
      )
    end

    it "routes put update" do
      expect(:put => "posts/1").to route_to(
        :controller => "posts",
        :action => "update",
        :id => "1" 
      )
    end

    it "routes delete destroy" do
      expect(:delete => "posts/1").to route_to(
        :controller => "posts",
        :action => "destroy",
        :id => "1"
      )
    end    
  end
end

If you want to write test for un routable routes than here is code for that

   resources :posts, :except => [:show]
  
    it " does not route to get show" do
      expect(:get => "posts/1").not_to be_routable
    end

You can place your routes tests inside controller folder but I created a new folder name routing. If you name your folder anything other than routing that you have to add a type to your describe like this

describe YourController, :type => :routing do
...
end

OR

describe YourController, :type => :controller do
...
end
Advertisements

One thought on “Testing Routes using Rspec

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s