我有一个使用 mongoid、database_cleaner 和 rspec 的现有项目。我尝试使用 active_admin patches available 添加 active_admin . ActiveAdmin 假设它在一个 ActiveRecord 项目中,特别是通过它对 meta_search gem 的依赖。
当我运行我的规范时,它们都失败并出现以下错误:
Failure/Error: Unable to find matching line from backtrace
ActiveRecord::ConnectionNotEstablished:
ActiveRecord::ConnectionNotEstablished
# ./spec/support/database_cleaner.rb:12:in `block (2 levels) in <top (required)>'
相关库的gem版本如下:
测试失败的文件,
spec/support/database_cleaner.rb
:require 'database_cleaner'
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.orm = "mongoid"
end
config.before(:each) do
DatabaseCleaner.clean
end
end
最佳答案
[移自问题]
似乎 database_cleaner 试图在 its initialization method 中自动检测可用的 ORM。
这可以通过更改 spec/support/database_cleaner.rb
来预先阻止。像这样的文件:
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner[:mongoid].strategy = :truncation
end
end
调用
[]
configuration 中的方法覆盖自动检测,从而不再添加 ActiveRecord。另一种解决方法是重新添加
config/database.yml
文件中的 sqlite3 配置被应用程序的其余部分忽略。谢天谢地,这没有必要。
关于ruby-on-rails - 使用 database_cleaner、mongoid 和 active_admin 会导致规范因 ActiveRecord::ConnectionNotEstablished 而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9559747/