LoginSignup
2
1

More than 3 years have passed since last update.

【Rails】RSpecでテストしたい。そんなあなたの一歩を応援します【導入手順】

Posted at

参考対象者

  • Rails6.0で、RSpecでテストしたいなと考えている方

環境

$ rails -v
Rails 6.0.3.1
$ ruby -v
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-darwin19]

RSpecを導入する

Gemfile
group :development, :test do
  gem 'rspec-rails'
end
$ bundle install

$ rails g rspec:install

gemをインストールし、設定ファイルをジェネレータで作成する。

.rspec
--require spec_helper
--format documentation

テストをドキュメント形式に設定する。

System Specを導入する ブラウザテスト

Gemfile
group :test do
  gem 'capybara', '>= 2.15'
  gem 'webdrivers'
end
$ bundle install

まずは、gemをインストールする。

spec/rails_helper.rb
RSpec.configure do |config|
# 一番下の直前に追加
  config.before(:each) do |example|
    if example.metadata[:type] == :system
      driven_by :selenium, using: :headless_chrome, screen_size: [1400, 1400]
    end
  end
end

ブラウザテストが機能するように、RSpecの設定を変更する。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1