rspec with mongoid, devise, database_cleaner : ActiveRecord::ConnectionNotEstablished error

981 views Asked by At

i'm trying to use rspec with mongoid, devise, database_cleaner and i have this error

 Failure/Error: Unable to find matching line from backtrace
 ActiveRecord::ConnectionNotEstablished:
   ActiveRecord::ConnectionNotEstablished
 # /home/adham/.rvm/gems/ruby-2.1.5/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:541:in `retrieve_connection'
 # /home/adham/.rvm/gems/ruby-2.1.5/gems/activerecord-4.1.8/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
 # /home/adham/.rvm/gems/ruby-2.1.5/gems/activerecord-4.1.8/lib/active_record/connection_handling.rb:87:in `connection'
 # /home/adham/.rvm/gems/ruby-2.1.5/gems/activerecord-4.1.8/lib/active_record/fixtures.rb:499:in `create_fixtures'
 # /home/adham/.rvm/gems/ruby-2.1.5/gems/activerecord-4.1.8/lib/active_record/fixtures.rb:984:in `load_fixtures'
 # /home/adham/.rvm/gems/ruby-2.1.5/gems/activerecord-4.1.8/lib/active_record/fixtures.rb:957:in `setup_fixtures'
 # /home/adham/.rvm/gems/ruby-2.1.5/gems/activerecord-4.1.8/lib/active_record/fixtures.rb:806:in `before_setup'

my gemfile the testing group

group :development, :test do
    gem "factory_girl_rails", "~> 4.0"
  gem 'rspec-rails', '~> 3.1.0'
  gem 'spork', '~> 0.8.5'
  gem 'mongoid-rspec', '~> 2.0.0.rc1'
  gem 'capybara'
  gem 'database_cleaner'
  gem 'shoulda-matchers'
end

and my rails_helper.rb ==

ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require "mongoid"
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

RSpec.configure do |config|
    config.fixture_path = "#{::Rails.root}/spec/fixtures"
    config.use_transactional_fixtures = false
  config.include Mongoid::Matchers
  config.include FactoryGirl::Syntax::Methods
  config.infer_spec_type_from_file_location!
end

what should i do??

1

There are 1 answers

1
kuhn On BEST ANSWER

I was having that same error.

my installed gems with versions:

rails (4.2.0)
rspec (3.2.0)
rspec-core (3.2.2)
rspec-expectations (3.2.0)
rspec-mocks (3.2.1)
rspec-rails (3.2.1)
rspec-support (3.2.2)
mongoid-rspec (2.2.0)

I have tried to disable fixtures from all modes, including all variances of

RSpec.configure do |config|
  config.use_transactional_fixtures = false
end

And nothing solve that error.

After some research I tried to patch rails ActiveRecord to manually disable fixtures (since MongoID doesn't support this feature). And now Rspec works with MongoID like a charm!

Just create file spec/support/disable_active_record_fixtures.rb with this content:

module ActiveRecord::TestFixtures
  def before_setup
    super
  end

  def after_teardown
    super
  end
end

Don't forget to require this file in rails_helper.rb

If you are using DatabaseCleaner you need to create another support file with this content:

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

  config.before(:each) do
    DatabaseCleaner[:mongoid].start
  end

  config.after(:each) do
    DatabaseCleaner[:mongoid].clean
  end
end