Rails6にRSpec、FactoryBot、DatabaseCleanerを導入する
2020/08/30
思考停止した導入方法のメモ
手順
Gemのインストール
Gemfile
group :development, :test do
...省略...
gem 'database_cleaner-active_record'
gem 'factory_bot_rails'
gem 'rspec-rails', '~> 4.0.1'
end
bundle install
RailsにRSpecをインストール
bundle exec rails generate rspec:install
create .rspec
create spec
create spec/spec_helper.rb
create spec/rails_helper.rb
設定
spec/rails_helper.rb
RSpec.configure do |config|
...省略...
config.include FactoryBot::Syntax::Methods
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end