When attempting to follow example on database_cleaner's GitHub page, I encountered the following error from RSpec:
ActiveRecord::StatementInvalid:
SQLite3::SQLException: cannot start a transaction within a transaction: begin transaction
The configuration used in spec_helper.rb is:
require 'spork'
require 'database_cleaner'
Spork.prefork do
# .. snip
RSpec.configure do |config|
# .. snip
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
end
Spork.each_run do
end
The accepted answer makes all tests slower (when it's not needed) by truncating after each one.
Just add
config.use_transactional_fixtures = false
when using database_cleaner.
If you have both config.use_transactional_fixtures = true
and DatabaseCleaner.strategy = :transaction
you're going to start a transaction inside another transaction and that's not allowed.
I found the solution to be changing the entire strategy to :truncation
. Updated spec_helper:
require 'spork'
require 'database_cleaner'
Spork.prefork do
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
end
Spork.each_run do
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With