非rails应用程序中的DatabaseCleaner错误

时间:2012-07-30 10:00:53

标签: ruby sequel

我正在尝试让DatabaseCleaner在我使用Sequel的非rails应用程序上运行,但我遇到了各种各样的问题。看起来DB没有正确设置 -

RSpec.configure do |config|
  config.before(:suite) do    
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
    p DatabaseCleaner.connections
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end
end

运行rspec返回 -

ruby-1.9.2-p290/gems/database_cleaner-0.8.0/lib/database_cleaner/sequel/truncation.rb:12:in `clean': undefined method `url' for :default:Symbol (NoMethodError)
  from /Users/tim/.rvm/gems/ruby-1.9.2-p290/gems/database_cleaner-0.8.0/lib/database_cleaner/base.rb:39:in `clean_with'
  from /Users/tim/.rvm/gems/ruby-1.9.2-p290/gems/database_cleaner-0.8.0/lib/database_cleaner/configuration.rb:62:in `block in clean_with'
  from /Users/tim/.rvm/gems/ruby-1.9.2-p290/gems/database_cleaner-0.8.0/lib/database_cleaner/configuration.rb:62:in `each'
  from /Users/tim/.rvm/gems/ruby-1.9.2-p290/gems/database_cleaner-0.8.0/lib/database_cleaner/configuration.rb:62:in `clean_with'

这条线失败了 -

case db_type= db.url.sub(/:.+/,'').to_sym

如果我删除'DatabaseCleaner.clean_with(:truncation)',我会得到以下内容,请注意数据库设置为:default而不是:test

[#<DatabaseCleaner::Base:0x007fdbe41825f0 @autodetected=true, @orm=:sequel, @strategy=#<DatabaseCleaner::Sequel::Transaction:0x007fdbe416b328 @db=:default>>]

An error occurred in an after hook
NoMethodError: undefined method `resume' for nil:NilClass occurred at /Users/tim/.rvm/gems/ruby-1.9.2-p290/gems/database_cleaner-0.8.0/lib/database_cleaner/sequel/transaction.rb:22:in `clean'

这两个错误都表明数据库尚未设置,但我看不出原因。有什么想法吗?

3 个答案:

答案 0 :(得分:3)

这已在database_cleaner中修复(请参阅https://github.com/bmabey/database_cleaner/commit/adb0d438dbfabaea2588f3058b1e75914706e53d),但看起来并不存在包含该修补程序的新版本。在新版本发布之前,你必须使用git版本。

答案 1 :(得分:1)

我遇到了同样的问题。我刚刚在我的Gemfile中替换了database_cleaner,直到下一个版本出来。

group :test do
  # The release version does not support sequel properly. See
  # https://github.com/bmabey/database_cleaner/commit/e5cb8cea.
  # FIXME: Update this, when DatabaseCleaner 0.8.1 is released!
  gem 'database_cleaner', :git => "git://github.com/bmabey/database_cleaner.git", 
    :ref => "e5cb8cea"
end

答案 2 :(得分:0)

  

我没有明确定义与DatabaseCleaner的[连接],我无法在文档中看到如何执行此操作。

您可以将现有连接传递给DatabaseCleaner,如下所示:

connection = Sequel.connect("mysql2://user:password@host:port/database_name")
DatabaseCleaner[:sequel, { :connection => connection) }]