RSpec preparation

This post is a memorandum of self-study.

After studying the railstutorial, I decided to rewrite the test from minitest to RSpec, which was the reason I tried to write this article.

This is my first post, but I hope this post is a hint for those who have read it.

Options for rails new

$ rails new --skip-test --skip-bunlde

Skip the default minitest and bundle install.

Add gem

Gemfile


group :development, :test do
  gem 'rspec-rails'
  gem 'spring-commands-rspec'
  gem 'capybara'
  gem 'factory_bot_rails'
  gem 'faker'
  gem 'selenium-webdriver'
  gem 'database_cleaner'
end
$ bundle install --without production --path=vender/bundle

↑ Not to be confused with the gem of a system without a PC

Installation of initial files for RSpec

$ rails g rspec:install
Running via Spring preloader in process 9045
      create  .rspec
      create  spec
      create  spec/spec_helper.rb
      create  spec/rails_helper.rb

Introduction of RSpec using Spring

RSpec execution time can be shortened by using spring. Use the gem'spring-commands-rspec' we added earlier for that. Create an RSpec stub file (./bin/rspec).

$ bundel exec spring binstub rspec

A bin/rspec file will be created and you can use spring by running RSpec.

*'Spring-commands-rspec': Required when executing bin/rspec commands. By adding a bin/command, RSpec can start an application called Spring, which is built into Rails, and speed up the process. RSpec itself can be used just by typing rspec.

Edit config/environments/test.rb

config/environments/test.rb


config.active_support.deprecation = :stderr
↓ Change to the setting of where to output the deprecated report
config.active_support.deprecation = :silence

For the time being, run rails db: migrate RAILS_ENV = test

$ rails db:migrate RAILS_ENV=test

Edit .rspec file

.rspec


--require spec_helper
--format documentation :Words that have passed the test will be displayed
--color

That's it for this time! How to write an article on Qiita It's quite unique I will do my best to post at least 2 articles a month! I'll do my best, 2021!

Recommended Posts

RSpec preparation
RSpec setup
Introducing RSpec
Install RSpec
RSpec Setup
RSpec Basics
Rspec, TDD (1)
Hello RSpec
Call API [Preparation]
Rspec introduction memo_Rails
Rspec Basics [Rails]
[Rails5] Rspec -validation-
About RSpec (Rails)
[Rails] From test preparation to model unit testing [RSpec]