SQLite3 :: SQLException when using database_cleaner with Rails / Spork / RSpec

While trying to follow the example on the database_cleaner GitHub page, I found 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:

 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 
+7
source share
2 answers

I found a solution to change the whole 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 
+6
source

The accepted answer makes all tests slower (when it is not needed) by truncating after each of them.

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 must start the transaction inside another transaction and this is not allowed.

+9
source

All Articles