RailsにRSpecを導入する手順
everyday Rails の第2章より必要な要素だけ抽出
目次
- RSpecの導入
- 出力フォーマットを読みやすくする
- binstubを使い、起動時間を短くする
- テスト用のファイルを自動生成する設定
RSpecの導入
Gemfile
group :development, :test do # ... gem 'rspec-rails', '~> 3.6.0' end
書き換えたら bundle install
binstubを使い、起動時間を短くする
Gemfile
group :development do # ... gem 'spring-commands-rspec' end
binstubの設定ファイルを作成
bundle install bundle exec spring binstub rspec
起動確認
bin/rspec
テスト用のファイルを自動生成する設定
下記の設定だとモデルとコントローラのスペックが自動で生成される。
config/application.rb
module Projects class Application < Rails::Application config.load_defaults 5.1 config.generators do |g| g.test_framework :rspec, fixtures: false, view_specs: false, # フィーチャースペックやシステムテストがあるため不要 helper_specs: false, routing_specs: false # routingが複雑になってきたらちゃんとテストしよう end end end
オススメの追加設定
下記記事の設定を追加すると、FactoryBot.create(:user)をcreate(:user)と書くことができるので楽
forest-valley17.hatenablog.com