概要
前提条件
手順
Gemfile
Gemfile
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
+ gem 'rspec-rails'
+ # Adds support for Capybara system testing and selenium driver
+ gem 'capybara', '>= 2.15'
+ gem 'factory_bot_rails'
end
group :development do
︙
gem 'spring-watcher-listen', '~> 2.0.0'
+ gem 'spring-commands-rspec'
end
group :test do
# Adds support for Capybara system testing and selenium driver
- gem 'capybara', '>= 2.15'
gem 'selenium-webdriver'
- # Easy installation and use of chromedriver to run system tests with Chrome
- gem 'chromedriver-helper'
+ gem 'webdrivers'
end
chromedriver-helper を削除し webdrivers を追加する理由は、rspecコマンド実行時に 下記のWarning が出るため
2019-08-15 07:18:52 WARN Selenium [DEPRECATION] Selenium::WebDriver::Chrome#driver_path= is deprecated. Use Selenium::WebDriver::Chrome::Service#driver_path= instead.
Gem インストール
$ bundle install
RSpecインストール
$ bundle exec rails g rspec:install
RSpec 設定変更
出力形式を見やすいドキュメント形式に変更
.rspec
--require spec_helper
+ --format documentation
rails g コマンド実行時のテストに関する設定の変更
config/application.rb
module Myapp
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.2
# Settings in config/environments/* take precedence over those specified here.
# Application configuration can go into files in config/initializers
# -- all .rb files in that directory are automatically loaded after loading
# the framework and any gems in your application.
+ config.generators do |g|
+ g.test_framework :rspec,
+ fixtures: true,
+ view_specs: false,
+ helper_specs: false,
+ routing_specs: false,
+ request_specs: true,
+ system_specs: true
+ end
end
end
Capybaraの初期セットアップ
spec/spec_helper.rb
+ require 'capybara/rspec'
RSpec.configure do |config|
+ config.before(:each, type: :system) do
+ driven_by :selenium_chrome_headless
+ end
︙
end
binstub 作成
$ bundle exec spring binstub rspec
動作確認
$ bundle exec bin/rspec
Running via Spring preloader in process 22
No examples found.
Finished in 0.00272 seconds (files took 0.68623 seconds to load)
0 examples, 0 failures