7

尝试在 database_cleaner 的GitHub 页面上执行示例时,我遇到了来自 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
4

2 回答 2

9

接受的答案通过在每个测试之后截断,使所有测试变慢(当不需要时)。

只需添加

config.use_transactional_fixtures = false

使用 database_cleaner 时。

如果您同时拥有两者config.use_transactional_fixtures = true,并且DatabaseCleaner.strategy = :transaction您将在另一个事务中启动事务,那么这是不允许的。

于 2013-06-03T20:44:15.250 回答
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
于 2012-08-31T18:39:40.243 回答