当试图在
database_cleaner的
GitHub page上关注示例时,我从RSpec遇到以下错误:
ActiveRecord::StatementInvalid: sqlite3::sqlException: cannot start a transaction within a transaction: begin transaction
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
解决方法
接受的答案通过在每个测试之后截断来使所有测试变慢(当不需要时).
只需添加
config.use_transactional_fixtures = false
使用database_cleaner时.
如果您同时拥有config.use_transactional_fixtures = true和DatabaseCleaner.strategy =:transaction,那么您将在另一个事务中启动一个事务,而这是不允许的.