Help us understand the problem. What is going on with this article?

RSpec 設定

RSpec導入のための備忘録です。

Gemfile

Gemfile
source 'https://rubygems.org'
ruby '2.6.3'
gem 'rails', '~> 6.0.0.rc1'

group :development, :test do
  gem 'factory_bot_rails'
end

group :test do
  gem 'capybara'
  gem 'selenium-webdriver'
  gem 'webdrivers'
  gem 'rspec-rails'
end
$ bundle install
$ rm -rf ./test
$ rails g rspec:install
config/application.rb
require_relative 'boot'
require 'rails/all'
Bundler.require(*Rails.groups)

module Taskleaf
  class Application < Rails::Application
    config.load_defaults 6.0

    config.generators do |g|
      g.test_framework :rspec,
                       fixtures: true,
                       view_specs: false,
                       helper_specs: false,
                       routing_specs: false,
                       controller_specs: false,
                       request_specs: true,
                       system_specs: true
    end
  end
end
spec/spec_helper.rb
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
require 'capybara/rspec'

RSpec.configure do |config|
  config.before(:each, type: :system) do
    driven_by :selenium_chrome_headless
  end

  # rspec-expectations config goes here. You can use an alternate
  # assertion/expectation library such as wrong or the stdlib/minitest
  # assertions if you prefer.
  config.expect_with :rspec do |expectations|
spec/rails_helper.rb
# コメントされているので、コメントを外す
# spec/support以下に、いろいろファイルを置けるようになる
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }


RSpec.configure do |config|
  # テスト中に、FactoryBot.create(:user) ではなく、create(:user) と書けるようになる。
  config.include FactoryBot::Syntax::Methods
end
spec/support/login_support.rb
module LoginSupport
  # 利用者がログインする
  def login_as(user)
    visit root_path
    click_link "Sign in"
    fill_in "Email", with: user.email
    fill_in "Password", with: user.password
    click_button "Log in"
  end
end

# RSpecの設定 LoginSupport をinclude
RSpec.configure do |config|
  config.include LoginSupport
end

RSpecのレポートを見やすく

.rspec
--require spec_helper
--format documentation

ファクトリ追加

rails g factory_bot:model user
spec/factories/users.rb
FactoryBot.define do
  factory :user do
    name { '太郎' }
    sequence(:email) { |n| "user#{n}@example.com" } # 連番
    password { 'password' }
  end
end

スペック作成

# rails g rspec:system users # ジェネレータ無いので手動
rails g rspec:request users
rails g rspec:model users

参考

使い方

使えるRSpec入門・その1「RSpecの基本的な構文や便利な機能を理解する」
使えるRSpec入門・その2「使用頻度の高いマッチャを使いこなす」
使えるRSpec入門・その3「ゼロからわかるモックを使ったテストの書き方」
使えるRSpec入門・その4「どんなブラウザ操作も自由自在!逆引きCapybara大辞典」
rspec-rails 3.7の新機能!System Specを使ってみた

書籍

Everyday Rails - RSpecによるRailsテスト入門
Everyday Rails
現場で使える Ruby on Rails 5速習実践ガイド

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away