LoginSignup
9
8

More than 5 years have passed since last update.

RSpecの導入

Posted at

はじめに

RSpecをインストールしたのでその手順をメモします。

*環境
ruby : 2.5.1
rails : 5.2.2

導入手順

1. gemのインストール

Gemnfile
group :development, :test do
  # 省略
  gem 'rspec-rails'
end

bundle installします。

2. RSpecの設定

rails generate rspec:installすると結果は以下。

Running via Spring preloader in process 93674
      create  .rspec
      create  spec
      create  spec/spec_helper.rb
      create  spec/rails_helper.rb

次にRSpecの出力形式を設定します。形式の種類についてはProject: RSpec Core 2.6 --format optionを参照ください。
今回はドキュメント形式にします。

/rspec
--require spec_helper
--format documentation  # この一行を追加

3. springを使ってRSpecの起動時間を速くする

Gemnfile
group :development do
  # 省略
  gem 'spring-commands-rspec'
end

bundle installし、次にbundle exec spring binstub rspecをするとbinディレクトリにrspecファイルが生成されます。

bin/rspec
#!/usr/bin/env ruby
begin
  load File.expand_path('../spring', __FILE__)
rescue LoadError => e
  raise unless e.message.include?('spring')
end
require 'bundler/setup'
load Gem.bin_path('rspec-core', 'rspec')

bin/rspecもしくはbundle exec rspecを実行して以下の通り結果がでればrspecは正常にインストールされています。

Running via Spring preloader in process 93755
No examples found.


Finished in 0.00079 seconds (files took 0.2886 seconds to load)
0 examples, 0 failures

4. ジェネレーターを(お好み)設定

rails gコマンドを実行した際に自動生成されるファイルについて、生成の要否を各自お好みで設定します。

config/application.rb
# 省略
module アプリ名
  class Application < Rails::Application

    # 省略

    config.generators.test_framework      = :rspec
    config.generators.system_tests        = false
    config.generators.stylesheets         = false
    config.generators.javascripts         = false
    config.generators.helper              = false
    config.generators.helper_specs        = false
    config.generators.view_specs          = false
    config.generators.controller_specs    = false
  end
end

その他

Factory Botのインストール

テスト用のデータを簡単に作成できるFactory Botを導入します。

Gemnfile
group :development, :test do
  # 省略
  gem 'factory_bot_rails'
end
/spec/rails_helper.rb
RSpec.configure do |config|
  # 省略
  config.include FactoryBot::Syntax::Methods
end

ファクトリを作成するにはbin/rails g factory/bot:model モデル名を実行します。

Capybaraの設定

Capybaraを導入することで、Webアプリのブラウザ操作をシミュレーションすることができます。
Rails5.1からデフォルトで同梱されていますが、RSpecでCapybaraを扱えるようにするために以下の一行を追加する必要があります。

/spec/rails_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!
require 'capybara/rspec'  # この一行を追加
end
9
8
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
9
8