Rspec + Rails: Setting up

Rspec + Rails: Setting up

enmanuelm19@gmail.com2018-07-30 19:20:52 UTC

Setting up Rspec in a Rails application


This post is the first in a serie of posts in which we see how to use Rspec to test the feeatures of our application. We will install the necesary dependencies in order to setting up the test enviroment.

The gems we are going to use are:

  • rspec-rails: It handles the environment to test the application.
  • FactoryBot: This gem takes care of generate the data according the design of our application.
  • FFaker: With FactoryBot it takes care of generate random test data for our application.
  • shoulda-machers: It bring useful comparators, it makes easier for us to test some features.
  • Database-cleaner: Erase the data every time that we run our test, in this way the database is clean for the next run.
  • Capybara: This gem simulates the interaction of a person with the application through the web browser.
  • rails-controller-testing: This gem brings back the assign method to our controllers. Is not necessary to install if you are working with Rails version 4 or less.
  • Selenium-Webdriver: It handles the interaction between the web browser and our application.
  • SimpleCov: This gem help us to see what percentage of our code have been tested.
Installation

Our Gemfile

group :test do
  gem 'capybara', '>= 2.15', '< 4.0'
  gem 'chromedriver-helper'
  gem 'database_cleaner'
  gem 'factory_bot_rails'
  gem 'ffaker'
  gem 'rails-controller-testing'
  gem 'rspec-rails'
  gem 'selenium-webdriver'
  gem 'shoulda-matchers'
  gem 'simplecov', require: false
end

Run bundle install in order to install the new dependencies added to our project.

Configuration

To use Rspec and the rest of the gems we have to run the following command:

rails generate rspec:install

This command creates thespec/ in the root of our project, inside this folder and following the naming conventions of rspec we should create the folders models/,controllers/,factories/,features/ in order to test our application.

To set up the integration of this gems in the file spec/rails_helper.rb we must put the following lines:

RSpec.configure do |config|
  .
  .
  # Only if is Rails 5
  [:controller, :view, :request].each do |type|
    config.include ::Rails::Controller::Testing::TestProcess, :type => type
    config.include ::Rails::Controller::Testing::TemplateAssertions, :type => type
    config.include ::Rails::Controller::Testing::Integration, :type => type
  end
  .
  .
  config.include FactoryBot::Syntax::Methods
  .
  .
  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
  end
end

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end

With this we have concluded this first part of the series. In the next post we will see how to generate test data and the most common cases of tests in our models. I hope this info were useful to you. Any doubt, advice or correction you can comment below.


Share