設定がおかしくて盛大にハマったので、メモを残しておきます。
ついでに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
今考えると名前そのままの動きですね