In this article, I will add a Hello World page to our existing rails project. I am assuming that you have already installed Rails 5 and RSpec. To know how to install RSpec in a Rails project, you may check this article.
First thing first, create a pages_controller. We can do this by running the Rails generator command in the command line. The advantage of creating the file with Rails generator is, it will automatically create the RSpec test files.
Okay, now run the following command in the terminal at the location of rails root –
rails g controller pages
It will output the following data in the command line –
create app/controllers/pages_controller.rb invoke erb create app/views/pages invoke rspec create spec/controllers/pages_controller_spec.rb invoke helper create app/helpers/pages_helper.rb invoke rspec invoke assets invoke coffee create app/assets/javascripts/pages.coffee invoke scss create app/assets/stylesheets/pages.scss
Now notice, it has created spec/controllers/pages_controller_spec.rb file where we will be able to write test codes for pages_controller.
We have not created the hello world functionality yet. Let’s do that first –
Modify app/controllers/pages_controller.rb file as following –
class PagesController < ApplicationController def home end end
Now create another file in the location app/views/pages/home.html.erb
For simplicity, lets the content of the file app/views/pages/home.html.erb be ‘Hello World!’ –
Hello World!
We have to change the routes to point to the home action. Let it be the root URL of the project.
Change the file config/routes.rb as following –
Rails.application.routes.draw do root to: 'pages#home' end
Now start the Rails server on your local machine by writing the following line in the console –
rails s
It will start the server with the following type of output –
=> Booting Puma => Rails 5.2.2.1 application starting in development => Run `rails server -h` for more startup options Puma starting in single mode... * Version 3.12.0 (ruby 2.3.1-p112), codename: Llamas in Pajamas * Min threads: 5, max threads: 5 * Environment: development * Listening on tcp://0.0.0.0:3000 Use Ctrl-C to stop
On your browser, if you go to the address http://localhost:3000/, it will show a page with the output ‘Hello World!’.
It’s time to add some RSpec test codes –
If you are using Rails 5, then make sure the gem ‘rails-controller-testing’ is added to your Gemfile on the development and testing group.
In your Gemfile –
group :development, :test do gem 'rails-controller-testing' end
Now add the test code for pages_controller in the file spec/controllers/pages_controller_spec.rb. After adding the test code, the file will look like this –
require 'rails_helper' RSpec.describe PagesController, type: :controller do describe 'GET home' do it 'renders the home template' do get(:home) expect(response).to render_template('home') end end end
Later, I will discuss more the ‘describe‘ and ‘it‘ method. RSpec defines them. They allow us to represent the test codes in a human-readable way.
The line ‘get(:home)‘ simulates a get request to the home method.
The line ‘expect(response).to render_template(‘home’)’ means that, the response from the controller should render the template ‘home‘. In this case, the controller should respond with the template ‘app/views/pages/home.html.erb‘.
Although we have written test code specific to the controller, Rails encourages us to write feature test codes.
I am going to write a feature test code for our hello world example with the help of RSpec.
At first I am going to create a new file spec/greetings_spec.rb. Then I will add the feature test to it –
require 'rails_helper' RSpec.describe 'Greet Viewer', type: :feature do scenario 'home page' do visit root_path expect(page).to have_content('Hello World!') end end
Now, I am going into it in more details about how it works.
Notice here ‘type: :feature‘ is used, as it is a feature test. This will enable us to use Capybara in this test code. Capybara works like a browser and does the tasks defined in the test code.
The word ‘scenario’ is used to describe the test label. It is the same as ‘it‘ that we have used in the previous controller test. We can even use ‘‘it’ instead of ‘‘scenario‘ in this test. However in feature tests ‘scenario‘ makes more sense.
‘visit root_path’ is used to instruct the capybara to hit the root_path in the browser.
expect(page).to have_content(‘Hello World!’) – This line explains itself.
Here ‘expect’ is an RSpec keyword which tells what the test code should expect to make it pass.
‘page’ returns the current page that is accessed by capybara. In this case, it will be the root path.
‘have_content’ is an RSpect method that checks if the expected element (in this case the page) have the desired content or not.
Now go to the command line and run –
rspec spec/greetings_spec.rb
The output will show that, the test is passed.
Leave a Reply