12
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RailsアプリのRSpecセットアップ方法

Last updated at Posted at 2019-05-26

はじめに

Rails初心者です。
RSpecを初めて触るので、初めに行うRSpecのインストール〜アプリケーション設定について、備忘録としてまとめます。

既存アプリに追加する前提です。
1から作る場合は、こちらが参考になります。
Railsアプリ作成手順まとめ

バージョン管理

ruby 2.7.0
rails 5.1.6

RSpecセットアップ

①RSpecインストール

1-1 Gemfileにrspec-railsを追加

Gemfile
group :development, :test do
  # 元からあるコードは省略

  gem 'rspec-rails', '~> 3.6.0'

end

1-2 bundle実行

terminal
$ bundle install

②テストデータベース確認

2-1 テスト用データベース確認

・SQLiteの場合

config/database.yml
test:
 <<: *default
database: db/test.sqlite3

・MySQL・PostgreSQLの場合

config/database.yml
test:
 <<: *default
database: アプリ名

があるはずなので確認。

もし書いてなければ、
config/database.ymlに上記のようなコードを追加

terminal
$ bin/rails db:create:all

でテストデータベース作成。

※余談ですが、 railsbin/rails の違いについては、
こちらが分かりやすいです。
Rails 4.1以降のコンソールコマンドは必ず bin/ を付けなきゃいけないの?

③RSpec設定

3-1 RSpecインストール

terminal
$ bin/rails generate rspec:install

これにより、

・RSpec用の設定ファイル(.rspec
・私たちが作成したスペックファイルを格納するディレクトリ(spec
・RSpecの動きをカスタマイズするヘルパーファイル

の3つが作成されます。

3-2 testディレクトリの削除

RSpecではspecディレクトリのspecファイルに書いてくので、Railsアプリケーション作成時に作られたtestファイルたちを削除します。

terminal
$ rm -r ./test

3-3 デフォルトの形式→ドキュメント形式へ変更

必須ではないですが、RSpecの出力結果を見やすくします。

.rspecファイルに下記を追加。

.rspec
--require spec_helper
--format documentation

3-4 binstubインストール

アプリケーションの起動時間を早くするSpringを追加します。

Gemfile
group :development do

  # 元からあるコードは省略

  gem 'spring-commands-rspec'

end
terminal
$ bundle install


新しいbinstubを作成

terminal
$ bundle exec spring binstub rspec

実行すると、
binディレクトリ内にrspecという実行用ファイルが作成されます。

④正常にRSpecがインストールできてるか確認

4-1 コマンド実行

terminal
$ bin/rspec


Running via Spring preloader in process 28279
No examples found.

Finished in 0.00074 seconds (files took 0.14443 seconds to load)
0 examples, 0 failures

このように出力されていれば成功です。

⑤テストファイル自動作成設定

5-1 ファイル作成に応じてテストファイルを作成

rails generateコマンドのようにジェネレータを使うと、
現時点で既に、
デフォルトのMinitestファイルがtestディレクトリには作成されず、
RSpecファイルがspecディレクトリに作成されます。
特に設定は不要です。

5-2 不要なテストファイルが作成されない設定

あとは、
不要なファイルが自動で作成されないように
config/application.rbに設定を加えます。

config/application.rb
require_relative 'boot'
require 'rails/all'

Bundler.require(*Rails.groups)

module Testapps # 自分が作成したアプリ名
  class Application < Rails::Application
    config.load_defaults 5.1

    # 元からあるコードは省略

    config.generators do |g|
      g.test_framework :rspec,
       fixtures: false, # テストDBにレコード作成するファイルの作成をスキップ(初めだけ、のちに削除)。
       view_specs: false, # ビューファイル用のスペックを作成しない。
       helper_specs: false, # ヘルパーファイル用のスペックを作成しない。
       routing_specs: false # routes.rb用のスペックファイル作成しない。
    end
  end
end

上のコードでは、
モデルスペックとコントローラスペックが
デフォルトで自動作成されるようになっています。

他に自動作成してほしいファイルがあれば、
上の該当するコードは書かなくて良いです。

参考

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

とても分かりやすいです。
引き続き勉強していきます。

12
11
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
12
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?