9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2014-05-01

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
9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?