DEV Community

Ilya N. Zykin
Ilya N. Zykin

Posted on

Testing Rails root route

Now I need to test a root route in a Rails App

Step 1. Learning the routing file

File: config/routes.rb

Rails.application.routes.draw do
  root to: 'root#index'
end
Enter fullscreen mode Exit fullscreen mode

Step 2. Adding a test

File: spec/routing/root_spec.rb. This time I didn't use describe, only it.

root_path is a Rails routing helper that is equal to /.

require 'rails_helper'

RSpec.describe 'Root Route', type: :routing do
  it 'leads to a correct controller and action' do
    get(root_path).should route_to("root#index")
  end

  it 'does not exist' do
    expect(post: root_path).not_to be_routable
  end
end
Enter fullscreen mode Exit fullscreen mode

Step 3. Running the spec

bundle exec rspec spec/routing/root_spec.rb --format documentation
Enter fullscreen mode Exit fullscreen mode

That is it!
Happy coding!

Top comments (0)

Heroku

This site is powered by Heroku

Heroku was created by developers, for developers. Get started today and find out why Heroku has been the platform of choice for brands like DEV for over a decade.

Sign Up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay