【RSpec】設定
RSpecの設定
RSpecとFactoryBotの導入をイチから
gemの追加
gemのバージョンは要確認。
# Gemfile group :development, :test do gem 'rspec-rails' # rspec本体 gem 'pry-byebug' # スペック内で'binding.pry'でデバッグできるようにする gem 'factory_bot_rails' # テストデータの生成に使用。consoleでも使えるように開発環境にも入れとく。 end group :test do gem 'capybara' # システムスペックのブラウザ操作に使用 gem 'webdrivers' # 使用するテスト用ブラウザを提供 end
$ bundle install
RSpecの設定
- 設定ファイルの生成
$ bin/rails g rspec:install Running via Spring preloader in process 53838 create .rspec create spec create spec/spec_helper.rb create spec/rails_helper.rb
- .rspecの設定
出力を見やすく整える
<!-- .rspec --> --require spec_helper --format documentation
- ジェネレータの設定
# config/application.rb config.generators do |g| g.test_framework :rspec, view_specs: false, helper_specs: false, routing_specs: false, controller_specs: false, model_specs: true, request_specs: false, fixtures: false end
FactoryBotの設定
省略記法を使用できるようにする
# spec/rails_helper.rb config.include FactoryBot::Syntax::Methods
capybaraの設定
# spec/rails_helper.rb require 'rspec/rails' Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f } # コメントアウトを戻す
ブラウザ設定
テスト毎にコメントアウトの切り替えでブラウザ表示の有無を変更する
# spec/support/capybara.rb RSpec.configure do |config| config.before(:each, type: :system) do # driven_by(:rack_test) # RSpecのデフォルト # driven_by(:selenium_chrome) # ブラウザ表示ON driven_by(:selenium_chrome_headless) # ブラウザ表示OFF end end