Skip to content

Instantly share code, notes, and snippets.

@shivabhusal
Last active December 15, 2015 03:53
Show Gist options
  • Save shivabhusal/b8cfda8496074ef3cbe1 to your computer and use it in GitHub Desktop.
Save shivabhusal/b8cfda8496074ef3cbe1 to your computer and use it in GitHub Desktop.
Clear and Good routing examples to teach how we can test routing specs/tests with RSpec

Newbie developers might wonder how they can test if their routes are in good conditions. They might not find good examples so I have pasted some examples from my code.

require 'rails_helper'

describe 'Routes Spec', type: :routing do
  describe 'Admin Section' do
    describe 'Articles Controller' do
      it "should route 'admin/articles/category', :to => 'articles#add_category'" do
        expect(post: 'admin/articles/category').to route_to('admin/articles#add_category')
      end

      it "should route 'admin/articles/add-image', :to => 'admin/articles#add_image'" do
        expect(post: 'admin/articles/add-image').to route_to('admin/articles#add_image')
      end
    end

    describe 'Reports Controller' do
      it 'should not have default routes enabled' do
        expect(post: '/admin/reports/').not_to be_routable
        expect(get: '/admin/reports/:id').not_to be_routable
        expect(get: '/admin/reports/edit').not_to be_routable
        expect(patch: '/admin/reports/:id').not_to be_routable
        expect(delete: '/admin/reports/:id').not_to be_routable
      end

      it 'should have custom routes enabled' do
        expect(get: '/admin/reports/').to be_routable
        expect(get: '/admin/reports/daily').to route_to('admin/reports#daily')
        expect(get: '/admin/reports/rewards').to route_to('admin/reports#rewards')
        expect(get: '/admin/reports/payouts').to route_to('admin/reports#payouts')
        expect(get: '/admin/reports/new-customers').to route_to('admin/reports#new_customers')
        expect(get: '/admin/reports/cancellations').to route_to('admin/reports#cancellations')
        expect(get: '/admin/reports/shipments').to route_to('admin/reports#shipments')
      end
    end

    # In this section `admin` is the namespace
    describe 'Users Controller' do
      it "should have custom routes defined" do
        expect(get: 'admin/customers').to route_to('admin/users#customers')
        expect(get: 'admin/staff').to route_to('admin/users#staff')
        expect(get: 'admin/admins').to route_to('admin/users#admins')
        expect(put: 'admin/users/notifications_all_read').to route_to('admin/users#notifications_all_read')
      end
    end

    ['payouts', 'users', 'pacts', 'articles', 'categories'].each do |controller|
      describe "#{controller.titleize} Controller" do
        it 'should have default action defined' do
          expect(get: "/admin/#{controller}/").to be_routable
          expect(post: "/admin/#{controller}/").to be_routable
          expect(get: "/admin/#{controller}/:id").to be_routable
          expect(get: "/admin/#{controller}/edit").to be_routable
          expect(patch: "/admin/#{controller}/:id").to be_routable
          expect(delete: "/admin/#{controller}/:id").to be_routable
        end
      end

    end
  end
end

Routes with slugs are handle like

require 'rails_helper'

describe 'Resource Routing', type: :routing do
  it 'should define the custom routes' do
    expect(get: 'resources/community').to route_to 'resources#community'
    expect(get: 'resources/customer_service').to route_to 'resources#customer_service'
    expect(get: 'resources/help').to route_to 'resources#help'
    expect(get: 'resources/tests').to route_to 'resources#tests'
    expect(get: '/resources/12').to route_to 'resources#category', category: '12'
    expect(get: 'resources/cat/slg').to route_to 'resources#show', category: 'cat', slug: 'slg'
    expect(get: 'resources/search').to route_to 'resources#search'
    expect(get: 'resources/kb').to route_to 'resources#knowledge_base'
    expect(get: 'resources/articles/slg').to route_to 'resources#show', slug: 'slg'
    expect(get: 'categories/cat').to route_to('resources#knowledge_base', category: 'cat')
  end
end

Important:

For routes with slugs like
get 'resources/kb', :to => 'resources#knowledge_base'
get 'resources/articles/:slug', :to => 'resources#show'
get 'resources/:category/:slug' => 'resources#show'
get 'resources/:category' => 'resources#category'
the position of routing rules you define matters; I mean if you change the order like
get 'resources/kb', :to => 'resources#knowledge_base'
get 'resources/articles/:slug', :to => 'resources#show'
get 'resources/:category' => 'resources#category'
get 'resources/:category/:slug' => 'resources#show'
Then the route with `:slug` wont work as every condition to match the very route always matches with the above one.

 

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