これまでに作った機能について簡単にまとめていく。
今回は、
Database Cleanerを使ったテストデータの削除
前提条件
Rails5系を使用していること
RSpecが導入されていること
まだの方はこちら
Gemの導入
group :test do gem 'database_cleaner' end
bundle installの実行
削除の設定
spec/spec_helper.rb
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.around(:each) do |example|
DatabaseCleaner.cleaning do
example.run
end
end
end
一連の処理truncationで設定しロールバックしてデータを削除する
対象のテーブルの指定
対象のテーブルを指定する場合は、以下のように指定する(省略可能)
DatabaseCleaner.strategy = :truncation, {:only => %w[blogs]}
以上で作成完了
