Rspecセットアップ
環境構築
Gemfile.rb group :development, :test do gem 'rspec-rails', '~> 5.0.0' gem 'factory_bot_rails' end
bundle install後rails g rspec:installコマンドで.rspec, spec_helper.rb, rails_helper.rbを生成
create .rspec create spec create spec/spec_helper.rb create spec/rails_helper.rb
.rspec --require spec_helper
他にも以下のように便利なものがあるらしい
出力結果を色分け
--color
rails_helperの読み込み
--require rails_helper
出力結果をドキュメント風に見やすくする
--format documentation
vendor/bundle以下に取得したRubyのライブラリをリモートリポジトリにプッシュしたくないので以下を記述
.gitignore /vendor/bundle
spec/support/配下のファイルを読み込むために、以下のコメントアウトを外す
rails_helper.rb Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }
以下の記述を追加してFactoryBot.をいちいち書かずにインスタンスを生成できるようにする
rails_helper.rb RSpec.configure do |config| config.include FactoryBot::Syntax::Methods end
特定のテストケースのみ実行できるようにするために、以下追記してfocus: trueを使えるように設定
spec_helper.rb RSpec.configure do |config| config.filter_run_when_matching :focus end
これにより、テストブロックにfをつけることでそのテストブロックのみテストが実行される
fdescribe, fcontext, fit のように使う
参考記事: