RSpecをインストール
group :development, :test do
  gem 'rspec-rails'
$ bundle install
$ bin/rails generate rspec:install
You can silence deprecations warning by setting the environment variable THOR_SILENCE_DEPRECATION.
   create  .rspec
   create  spec
   create  spec/spec_helper.rb
   create  spec/rails_helper.rb
.rspec(設定ファイル)、spec/spec_helper.rb、spec/rails_helper.rbが生成されます。
--require spec_helper
--format documentation #この行を追加して出力の形式を変更
binstub をインストールしてテストスイートの起動時間を速くします。
group :development do
  gem 'spring-commands-rspec'
$ bundle install
$ bundle exec spring binstub rspec
動作確認
$ bin/rspec
テストが走ってNo examples found.的な記述が確認できればOK。
次にrails generateコマンドを叩いた時にテスト用ファイルが自動で生成されるように修正。
config.generators do |g|
  g.stylesheets false
  g.javascripts false
  g.helper false
  g.test_framework :rspec,
    fixtures: true,
    request_specs: false,
    controller_specs: false,
    view_specs: false,
    helper_specs: false,
    routing_specs: false
end
最後にtestディレクトリを丸ごと削除してセットアップ完了。
と思ったら、コマンドを叩くたびにthorとかいう、入れた覚えのないgemから以下のエラーが吐かれるようになってしまった。さっき追加したspringと依存関係にある、シェルスクリプトを生成するためのgemらしい。
Deprecation warning: Expected string default value for '--test-framework'; got false (boolean).
This will be rejected in the future unless you explicitly pass the options `check_default_type: false` or call `allow_incompatible_default_type!` in your code
You can silence deprecations warning by setting the environment variable THOR_SILENCE_DEPRECATION.
指示通り環境変数をセットしてエラーを非表示に。dotenvのgemを使ってます。
THOR_SILENCE_DEPRECATION = true
FactoryBotをインストール
テストデータ生成用のGemFactoryBotをインストールします。
group :development, :test do
  gem 'factory_bot_rails'
$ bundle install
FactoryBot.create(:user)→create(:user)と書けるように設定を追加。
RSpec.configure do |config|
.
.
  config.include FactoryBot::Syntax::Methods
end
config.generators do |g|
  g.stylesheets false
  g.javascripts false
  g.helper false
  g.test_framework :rspec,
    fixtures: true,
    request_specs: false,
    controller_specs: false,
    view_specs: false,
    helper_specs: false,
    routing_specs: false
  g.fixture_replacement :factory_bot, dir: 'spec/factories' # 追加
end
Capybaraをインストール
統合テスト用のGemcapybaraと、関連するGemをインストールします。
group :test do
  gem 'capybara'
  gem 'webdrivers'
  # gem 'selenium-webdriver'
  # gem 'chromedriver-helper'
end
selenium-webdriver、chromedriver-helperは不要なので、もし書いてあったら削除します。
$ bundle install
rails_helper.rbを開き、以下の行をコメントアウトして有効化します。
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
これでspec/supportにRSpec関連の設定ファイルを配置することができます。
最後にcapybara用の設定ファイルを作成します。
Capybara.javascript_driver = :selenium_chrome_headless
shoulda-matchersをインストール
マッチャーを拡張するgemshoulda-matchersをインストールします。
group :test do
  gem 'shoulda-matchers'
end
$ bundle install
rails_helper.rbを開き、以下の記述を追加。
RSpec.configure do |config|
.
. 
end
Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end