2

我试图让 DatabaseCleaner 在我使用 Sequel 的非 Rails 应用程序上运行,但我遇到了各种各样的问题。看起来数据库设置不正确 -

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'

The line this is failing on -

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

If I remove 'DatabaseCleaner.clean_with(:truncation)' I get the following, notice the DB is set to :default and not :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'

Both errors indicate that the DB hasn't been set, but I can't see why. Any ideas?

4

3 回答 3

3

这已在 database_cleaner 中修复(请参阅https://github.com/bmabey/database_cleaner/commit/adb0d438dbfabaea2588f3058b1e75914706e53d),但看起来没有包含修复的新版本。在新版本发布之前,您必须使用 git 版本。

于 2012-07-30T15:30:00.973 回答
1

我有同样的问题。我只是database_cleaner在我的 Gemfile 中替换,直到下一个版本出来。

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
于 2012-07-30T22:26:04.153 回答
0

我没有用 DatabaseCleaner 明确定义 [a connection],我在文档中看不到如何做到这一点。

您可以像这样将现有连接传递给 DatabaseCleaner:

connection = Sequel.connect("mysql2://user:password@host:port/database_name")
DatabaseCleaner[:sequel, { :connection => connection) }]
于 2013-12-11T18:57:07.613 回答