Ruby on Rails Rspec Tutorial: Setup Rspec on Rails 5

, , Leave a comment

By default Rails 5 does not add Rspec. That is why at first we will have to set up Rspec.

I will assume you already have a Rails 5 setup.

Add the following lines to your Gemfile for installing Rspec and other dependent gems –

group :development, :test do
  gem 'rspec-rails'
  gem 'factory_girl_rails'

group :test do
  gem 'faker'
  gem 'capybara', '>= 2.15'
  gem 'selenium-webdriver'
  gem 'chromedriver-helper'
  gem 'guard-rspec'
  gem 'launchy'

And then run bundle from command line of the root of your project

bundle install

It will install rspec and other required gems.

Make sure your config/database.yml file is set up properly including the test database information.

Here is an example of my config/database.yml file. I have used postgres as my database. To know how to install postgres for your database you may check this link.

default: &default
  adapter: postgresql
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000
  username: kimran
  password: password
  socket: /var/run/postgresql
  host: localhost
  port: 5432

  <<: *default
  database: demo_rails_development

  <<: *default
  database: demo_rails_test

  <<: *default
  database: demo_rails_production

Now from command line run the following to create the database if you have not created them already –

rake db:create
rake db:migrate
rake db:migrate RAILS_ENV=test

Now setup Rspec to your rails project by running the followings in the command line –

rails g rspec:install

It will create some files and output the following lines –

Running via Spring preloader in process 11378
      create  .rspec
      create  spec
      create  spec/spec_helper.rb
      create  spec/rails_helper.rb

Now add the following line at the bottom of .rspec file –

--require rails_helper

Now at the top of the file spec/spec_helper.rb add the following line to add Capybara to Rspec. In the future, we will need Capybara to run integration tests.

require 'capybara/rspec'

When we run the generators from the command line, they try to install default rails minitest. We can set instruction in our rails app so that at the time of generating code rails generator will generate rspec codes.

To do that, we will have to add the following lines config/application.rb

config.generators do |g|
      g.test_framework :rspec,
                       fixtures: true,
                       view_specs: true,
                       helper_specs: false,
                       routing_specs: false,
                       controller_specs: true,
                       request_specs: true
      g.fixture_replacement :factory_girl, dir: 'spec/factories'

And now the work is done. Now if you run any generator it will generate rspec files.

For example, suppose I need to create a controller called PagesController. In that case I will run the following in the command line –

rails g controller pages

It will provide the following output –

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/
      invoke    scss
      create      app/assets/stylesheets/pages.scss

Here you can see it created all the files needed for Rspec as we have set the config on config/application.rb.

Okay now the setup is done. Now it is time to write test codes on rspec.


Leave a Reply