0

I have a Rails 4.2.8 app, ruby 2.4.4 with a Mongo database, using the mongoid ODB. I want to run tests against this application using rspec. The most basic test gives me this error: ActiveRecord::ConnectionNotEstablished: No connection pool for ActiveRecord::Base

My research on this problem lead me to these conclusions, but I do not know how to solve the problem itself:

  • why is active record mentionned here, when it is not in my gemfile ? Maybe another gem is loading it at some point, but how can i get rid of this problem ?
  • my application.yml and mongoid.yml file are correctly configured. I can RAILS_ENV=TEST rails console and I can query documents.
  • looks like rails is going inside the condition line 15 in this file. So how and why is ActiveRecord loaded at some point ?

Some help would really be appreciated as I do not know what to do next...

ypicard
  • 3,593
  • 3
  • 20
  • 34
  • I'm guessing some gem is trying to use ActiveRecord, you need to know what gem is doing that and check the docs or contact the developers. Check gemfile.lock, maybe is listed there. – arieljuod Oct 24 '18 at 01:42
  • Active record is the rails module that connects to database and update models. I am not sure running the console is a proof that your app connects to the database. You can check this thread to test https://stackoverflow.com/questions/30170025/ruby-test-whether-database-connection-is-possible – Maxence Oct 24 '18 at 01:46
  • Well I can query documents when in console in test env... – ypicard Oct 24 '18 at 01:50
  • @arieljuod how do i know which gem does that ? The only activerecord i see in my gemfile.lock is under rails (4.2.8) – ypicard Oct 24 '18 at 02:03
  • check this question https://stackoverflow.com/questions/19078044/disable-activerecord-for-rails-4#19078854, hope it helps – arieljuod Oct 24 '18 at 02:38
  • I would read this https://stackoverflow.com/questions/38176304/no-connection-pool-for-activerecordbase#43847743 maybe you did not set correctly the test database, can you open `rails c test`? how did you set up the `test` db in your `database.yml`? also check online how to setup your database with rails, mongoid and mongodb – Fabrizio Bertoglio Oct 24 '18 at 09:38

2 Answers2

3

Ok so I did not figure out who was loading ActiveRecord in my application, but I manually unload it in my rails_helper.rb:

Object.send(:remove_const, :ActiveRecord)
# Make sure it is above this
require 'rspec/rails'
ypicard
  • 3,593
  • 3
  • 20
  • 34
0

I ran into the same issue and found that the problem was database cleaner. Previously we were using the gem database_cleaner but that gem seems to have been split based on your database adapter. Switching to database_cleaner-mongoid resolved the issue. Note that you may need to change some requires or config values to get things running correctly after the change. For instance I had an error about my strategy not being supported in mongoid.