Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using database_cleaner, mongoid and active_admin causes specs to fail with ActiveRecord::ConnectionNotEstablished

I have an existing project using mongoid, database_cleaner and rspec. I try to add active_admin, using the active_admin patches available. ActiveAdmin assumes it is in an ActiveRecord project, most specifically via its dependence on the meta_search gem.

When I go to run my specs, they all fail with the following error:

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)>'

The gem versions of the related libraries are as follows:

  • activeadmin (0.4.2)
  • database_cleaner (0.7.1)
  • mongoid (2.4.5)
  • meta_search (1.1.3)
  • activerecord (3.2.1)

The file that the tests are failing on, 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
like image 575
dznz Avatar asked Mar 04 '12 23:03

dznz


1 Answers

[moved from question]

It appears that database_cleaner attempts to autodetect the ORMs available to it in its initialization method

This can be pre-empted by changing the spec/support/database_cleaner.rb file like so:

RSpec.configure do |config|
  config.before(:suite) do
    DatabaseCleaner[:mongoid].strategy = :truncation
  end
end

Calling the [] method in configuration overrides the autodetect such that ActiveRecord is no longer added.

Another workaround would have been to re-add a config/database.yml file with a sqlite3 configuration that the rest of the application ignored. Thankfully that isn't necessary.

like image 92
Félix Saparelli Avatar answered Oct 17 '22 02:10

Félix Saparelli