RSpec まとめ
###RSpecのインストールと準備 Geimgile
gem 'rspec-rails'
bundle install後、以下のコマンドで設定ファイルが作られる。
rails g rspec:install
設定ファイルをすべてのテストで利用するためにファイルの行頭には以下を必ず記述。
require 'spec_helper'
###実行 指定したファイルを実行
RAILS_ENV=test bundle exec rspec spec/controllers/user_controller_spec.rb
ディレクトリも指定可能
RAILS_ENV=test bundle exec rspec spec/controllers/
exampleも指定可能(キーワードは部分一致)
bundle exec rspec spec/controllers/user_controller_spec.rb -e 'キーワード'
###テスト項目一覧をドキュメント形式で出力
--dry-run: テストは実行しない
-f d: ドキュメント形式で出力
bundle exec rspec user_controller_spec.rb -f d --dry-run --order defined
###ランダム順序テストを再現する
rspec spec/models --seed [seed値]