Help us understand the problem. What is going on with this article?

非ActiveRecord環境でRails 4.1にアップデートするとActiveRecord::ConnectionNotEstablishedが出る

More than 5 years have passed since last update.

tl;dr

spec_helper.rbでモンキーパッチ読み込ませる

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)

require_relative "../lib/monkey_patches/active_record"
require 'rspec/rails'
# rspec-railsがActiveRecordに繋ぎにいくので無理やり止める
ActiveRecord.module_eval do
  remove_const :TestFixtures
end

問題

Mongoidのアプリケーションを、Rails 4.1にアップデートしたらspecを走らせるときに ActiveRecord::ConnectionNotEstablished が出るようになった。

ActiveRecordを使ってないはずなのに、ActiveRecordで繋ごうとしていて明らかにおかしい。

     Failure/Error: Unable to find matching line from backtrace
     ActiveRecord::ConnectionNotEstablished:
       ActiveRecord::ConnectionNotEstablished
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:541:in `retrieve_connection'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.1.0/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.1.0/lib/active_record/connection_handling.rb:87:in `connection'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.1.0/lib/active_record/fixtures.rb:499:in `create_fixtures'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.1.0/lib/active_record/fixtures.rb:985:in `load_fixtures'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.1.0/lib/active_record/fixtures.rb:958:in `setup_fixtures'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.1.0/lib/active_record/fixtures.rb:807:in `before_setup'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/rspec-rails-2.14.2/lib/rspec/rails/adapters.rb:68:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_eval_with_args'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:247:in `instance_eval_with_args'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:106:in `block (2 levels) in run'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:108:in `call'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:108:in `run'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:446:in `run_hook'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:340:in `run_around_each_hooks'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:256:in `with_around_each_hooks'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:111:in `run'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:390:in `block in run_examples'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:386:in `map'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:386:in `run_examples'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:371:in `run'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:372:in `block in run'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:372:in `map'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:372:in `run'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:28:in `map'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:28:in `block in run'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/reporter.rb:58:in `report'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:25:in `run'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:80:in `run'
     # /Users/zat/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:17:in `block in autorun'

解決策

何かの拍子でActiveRecordが読み込まれると、specがtestのfixturesを作ろうとしてしまうので、ActiveRecordをいなかったことにする。

これを読み込ませればよい。

# rspec-railsがActiveRecordに繋ぎにいくので無理やり止める
ActiveRecord.module_eval do
  remove_const :TestFixtures
end
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away