Configure RSpec and Capybara with Ruby on Rails
In this tutorial I’m going to show you how to configure your Rails app to use RSpec and Capaybara. In addition, we will also install and configure Factory Bot Rails and Database Cleaner, since these Gems help with a better testing experience.
1. Create a New App and Skip Tests
Since we’ll be using rspec-rails as the test framework, we need to skip the default Rails test suite.
-
Open up a new terminal and create a new Rails app. Be sure to pass the
-T
flag. This will skip the default test suite
2. Install and Configure RSpec Rails
rspec-rails is a testing framework for Rails 3.x, 4.x and 5.x. rspec-rails extends Rails’ built-in testing framework to support rspec examples for requests, controllers, models, views, helpers, mailers and routing.
TLDR: rspec-rails is a popular alternative to the default Rails test suite.
-
Add
gem 'rspec-rails'
to yourGemfile
in the:development, :test
group
. - In the terminal window, run
bundle install
- In the terminal window, run
rails generate rspec:install
per rspec-rails instructions
3. Install and Configure Factory Bot Rails
factory_bot is a fixtures replacement with a straightforward definition syntax, support for multiple build strategies (saved instances, unsaved instances, attribute hashes, and stubbed objects), and support for multiple factories for the same class (user, admin_user, and so on), including factory inheritance.
TLDR: factory_bot makes it easy to create sample data to test against
-
Add
gem 'factory_bot_rails'
to yourGemfile
in the:development, :test
group
. - In the terminal window, run
bundle install
- Create a new directory at
mkdir spec/factories/
by runningmkdir spec/factories
in the terminal window. This is required by factory_bot_rails - Create a new directory at
spec/support/
by runningmkdir spec/support
in the terminal window - Create
factory_bot.rb
inspec/support
by runningtouch spec/support/factory_bot.rb
in the terminal window -
Open
spec/support/factory_bot.rb
and add the following: - Open
spec/rails_helper.rb
and uncommentDir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }
.
4. Install and Configure Capybara
Capybara is a library written in the Ruby programming language which makes it easy to simulate how a user interacts with your application.
-
Add
gem 'capybara'
to yourGemfile
in the:development, :test
group
. - In the terminal window, run
bundle install
- Create
capybara.rb
inspec/support
by runningtouch spec/support/capybara.rb
in the terminal window -
Open
spec/support/capybara.rb
and add the following:
5. Install and Configure Database Cleaner
Database Cleaner is used to ensure a clean state for testing.
-
Add
gem 'database_cleaner'
to yourGemfile
in the:test
group
. - In the terminal window, run
bundle install
-
Open
spec/support/capybara.rb
and add the following: - Comment out
config.use_transactional_fixtures = true
fromspec/rails_helper.rb
6. Create Our First Test (Optional)
Now that our app’s test suite is configured with RSpec, Capybara, Factory Bot Rails and Database Cleaner, let’s write some tests to ensure everything is working.
- In the terminal window, run
rails g model post title body:text
to generate a newmodel
. - In the terminal window, run
rails db:migrate
to migrate the database. This should have generatedspec/models/post_spec.rb
andspec/models/post_spec.rb
. -
Update
spec/models/post_spec.rb
with the following code: -
Run
rspec
. The test should fail. -
Open
app/models/post.rb
and add the following validation: -
Run
rspec
. The test should pass. - This was just an exercise to ensure the test suite is running correctly. For more examples on how to configure your tests, I recommend looking at RSpec Rails Examples and Better Specs