2012-08-31 27 views
7

當試圖遵循例如在database_cleaner的GitHub page的時候,我遇到了從RSpec的以下錯誤:的SQLite3 ::使用的SQLException與database_cleaner滑軌/叉勺/ 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 
+0

創建記錄時出現同樣的錯誤,可能是您可以提供幫助嗎? http://stackoverflow.com/questions/14367396/sql-error-cannot-start-a-transaction-within-a-transaction-while-testing-with-cuc – pahnin

回答

6

我發現解決方案將整個策略改爲:truncation。更新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 
+0

優秀 - 感謝您的幫助! – bonhoffer

9

接受的答案,使所有的測試速度較慢(不是需要時)由每一個後截斷。

只需添加

config.use_transactional_fixtures = false 

使用database_cleaner時。

如果您同時擁有config.use_transactional_fixtures = trueDatabaseCleaner.strategy = :transaction,那麼您將在另一個事務中啓動事務,這是不允許的。

+0

過去一段時間,因爲我有這個問題。我想我可能已經嘗試過了。忘了哪個項目,所以我不能重新測試= \ – Dan

+0

@我以爲這可能發生了,但我回答爲未來的讀者保留一個記錄。 –

相關問題