LoginSignup
4
1

More than 5 years have passed since last update.

E2E テストでの DatabaseCleaner の設定

Posted at

設定がおかしくて盛大にハマったので、メモを残しておきます。
ついでにjavascript_driverの設定も書いておきます。

環境

  • Ruby: 2.2.4
  • Rails: 4.2.5
  • Capybara: 2.5.0
  • Poltergeist: 1.9.0
  • DatabaseCleaner: 1.5.2

rails_helper.rb

各 spec が feature の時だけトランケーションを使い、それ以外の時はトランザクションを使うようにしています。

spec/rails_helper.rb
:
:
require "capybara/poltergeist"
Capybara.javascript_driver = :poltergeist

RSpec.configure do |config|
  :
  : 
  config.use_transactional_fixtures = false
  :
  :
  config.before :each do
    DatabaseCleaner.strategy = :transaction
  end

  config.before :each, type: :feature do
    DatabaseCleaner.strategy = :truncation, {
      except: %w(some master tables)
    }
  end

  config.before :each do
    DatabaseCleaner.start
  end

  config.after :each do
    DatabaseCleaner.clean
  end
end

余談

実はDatabaseCleaner.clean_withの使い方を盛大に勘違いしててハマりました。

At times you may want to do a single clean with one strategy.

For example, you may want to start the process by truncating all the tables, but then use the faster transaction strategy the remaining time. To accomplish this you can say:

require 'database_cleaner'

DatabaseCleaner.clean_with :truncation

DatabaseCleaner.strategy = :transaction

# then make the DatabaseCleaner.start and DatabaseCleaner.clean calls appropriately

DatabaseCleaner/database_cleaner

今考えると名前そのままの動きですね :sweat_smile:

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1