加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 在使用带有Rails / Spork / RSpec的database_c

发布时间:2020-12-17 04:22:39 所属栏目:百科 来源:网络整理
导读:当试图在 database_cleaner的 GitHub page上关注示例时,我从RSpec遇到以下错误: ActiveRecord::StatementInvalid: SQLite3::SQLException: cannot start a transaction within a transaction: begin transaction spec_helper.rb中使用的配置是: require 's
当试图在 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,那么您将在另一个事务中启动一个事务,而这是不允许的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读