[RAILS] Install RSpec


Install RSpec by referring to RSpec Kotohajime ~ Initial Setting Manual ~ and the official document.


See this article for Rails installation. Building Rails environment with WSL of Windows 10

RSpec installation

See also: rspec / rspec-rails Follow the official procedure to install. First, edit the Gemfile.


group :development, :test do
  gem 'rspec-rails', '~> 4.0.1'

After editing the Gemfile, execute the following command directly under the project directory.


#RSpec installation
$ bundle install

#Creating a standard configuration file
$ rails generate rspec:install
Running via Spring preloader in process 266
      create  .rspec
      create  spec
      create  spec/spec_helper.rb
      create  spec/rails_helper.rb

A directory for RSpec and a configuration file have been created. From now on, RSpec will be used, so delete the / test directory for Minitest.


#Delete test directory
$ rm -rf test

Initial setting

Add the settings to the created .rspec file.


--require spec_helper
--format documentation
Option name Explanation
require Specifying the file to call
coler Color the output
format Output format setting

Reference: Command line --RSpec Core --RSpec --Relish

This setting eliminates the need to include require'spec_helper' in the spec file. The output result is not just a dot, but a sentence format. This completes the basic settings.

Test system Gem

For the time being, just put on the list. Fake services will be added later.

Gem name URL
factory bot thoughtbot/factory_bot_rails
rubocop-rspec rubocop-hq/rubocop-rspec
webmock bblimke/webmock

Bonus about database_cleaner

Run Rails 5.1 or later system tests with RSpec (translation) As mentioned in the referenced article, database_cleaner seems to be unnecessary after Rails 5.1. However, it seems that the rollback mechanism is implemented in System Spec instead of installing database_cleaner automatically. In the first place


RSpec.configure do |config|
  config.use_transactional_fixtures = true

Reference: Transactions --RSpec Rails --RSpec --Relish With the above settings, database operations performed by RSpec will be processed in a transaction and removed by rollback. The reason why database_cleaner was introduced

[Translation + α] Use database_cleaner gem with Rails / RSpec / Capybara / Selenium This is because the above settings work effectively in the RSpec process, and did not work effectively if it was a user-operated emulation (Feature Spec) using Capybara / Selenum, etc. Therefore, I want to set the DB cleanup in detail at the time of testing! You don't need database_cleaner unless you're still using the Feedback Spec instead of the System Spec.

Recommended Posts

Install RSpec
RSpec setup
Introducing RSpec
RSpec preparation
RSpec Setup
RSpec Basics
Rspec, TDD (1)
Hello RSpec
OwlgardenRPA --Install
Install qiita_org
Install Apache JMeter
Install MariaDB (CentOS 8)
[CentOS] Install apache-loggen
bundle install error
Rspec Basics [Rails]
composer install error
[Rails5] Rspec -validation-
About RSpec (Rails)