How to Install the RSpec and Capybara Testing Frameworks
In order to follow Ruby on Rails best practices, in this guide we'll install and configure the RSpec and Capybara testing framework libraries.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

In this guide we are going to setup our automated testing environment by installing RSpec and Capybara.

Let's start with our Gem file. When you open this file, look for group :development, :test do. Copy the following code to it.

# Gemfile

group :development, :test do
  gem 'byebug'
  gem 'rspec-rails', '~> 3.0'
  gem 'capybara'
  gem 'database_cleaner'
end

If you don't like all the comments inside your Gem file, you can always find the # sign and delete all of them.

large

Next, go to your command line and type bundle. This will bring up all the dependencies we added:

medium

Now, let's see what we added:
- RSpec, which is our testing framework.
- Capybara which allows us to mimic browser sessions, so our test can login and run through the full list of features, just like we'd do in a browser.
- database_cleaner which will clean all the data after every test run, so we don't have to worry about manually deleting all the records.

Now that we've bundled all the Gem files together, let's install RSpec with the command:

rails generate rspec:install

large

This reminds me of something that I should've done earlier. Go to sublime editor and delete the test directory as it is considered bad practice to have both rspec and the default Rails testing libraries in place at the same time.

I remember one legacy app I took over that had both frameworks. I wasted two days building out new tests using the Rails default framework because the previous developer forgot to delete it. Needless to say I wasn't happy and neither was the client.

Next, we need to clean up a file called rails_helper.rb that was created by the rspec:install command. Open this file, delete all its contents, and copy the following code into it.

# spec/rails_helper.rb

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)

abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'capybara/rails'

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = false
  config.before(:suite) { DatabaseCleaner.clean_with(:truncation) }
  config.before(:each) { DatabaseCleaner.strategy = :transaction }
  config.before(:each, :js => true) { DatabaseCleaner.strategy = :truncation }
  config.before(:each) { DatabaseCleaner.start }
  config.after(:each) { DatabaseCleaner.clean }
  config.infer_spec_type_from_file_location!
  config.filter_rails_from_backtrace!
end

In this code, I'm essentially setting up my test configurations so that it can include database_cleaner and capybara.

In the next guide, we'll create our first page.

Resources