14

我们在带有数据库清理器的测试套件中随机收到以下错误。我们将数据库清理器与以下两个我认为相关的代码片段结合使用:

错误

Mysql2::Error: This connection is in use by: #<Thread:0x00000017bbf2f8 sleep>: TRUNCATE TABLE `cr_contacts`;

共享数据库连接(可能的原因)

class ActiveRecord::Base
  mattr_accessor :shared_connection
  @@shared_connection = nil

  def self.connection
    @@shared_connection || retrieve_connection
  end
end

# Forces all threads to share the same connection. This works on
# Capybara because it starts the web server in a thread.
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
4

1 回答 1

9

该错误告诉您 DB 清理器想要在其他线程仍在使用连接时进行截断。这可能发生在您与 selenium 的集成测试中。

看起来您正在使用 2 种数据库清理机制:

  • 具有共享连接补丁的事务性固定装置
  • 使用数据库清理器截断

这不行。要使用 DB 清理器,请关闭事务装置并删除共享连接补丁。我建议按照 Avdi Grimm 在这篇精彩文章中描述的方式配置它:http: //devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/

于 2013-08-24T15:00:18.220 回答