ruby-on-rails – 带有mongoid,devise,database_cleaner的rspec:ActiveRecord :: ConnectionNotEstablished error

我正在尝试使用rspec与mongoid,devise,database_cleaner,我有这个错误

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'

我的gemfile是测试组

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

和我的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

我该怎么办??

解决方法

我有同样的错误.

我安装的宝石版本:

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)

我试图从所有模式中禁用灯具,包括所有模式

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

没有什么能解决这个错误.

经过一些研究后,我尝试修补rails ActiveRecord来手动禁用灯具(因为MongoID不支持功能).而现在Rspec与MongoID一起工作就像一个魅力!

只需使用以下内容创建文件规范/ support / disable_active_record_fixtures.rb:

module ActiveRecord::TestFixtures
  def before_setup
    super
  end

  def after_teardown
    super
  end
end

不要忘记在rails_helper.rb中要求此文件

如果您使用的是DatabaseCleaner,则需要使用以下内容创建另一个支持文件

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

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...