LoginSignup
1
1

More than 5 years have passed since last update.

Spork導入してRSpec動かそうとした時にActiveRecord::ConnectionNotEstablishedでハマった

Posted at

エラー名

rspec --drb spec で実行すると下のようなエラーが出た。

ActiveRecord::ConnectionNotEstablished

対処法

Spork.prefork do
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'rspec/autorun'

  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 = true
    config.infer_base_class_for_anonymous_controllers = false
    config.order = "random"
    config.include Capybara::DSL
    config.expect_with :rspec do |c|
      c.syntax = :expect
    end
  end
end

Spork.each_run do
  ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection  # 多分ここが重要らしい!!
end

ファイルを追加する

spec/support/shared_connection.rb
class ActiveRecord::Base
  mattr_accessor :shared_connection
  @@shared_connection = nil

  def self.connection
    @@shared_connection || retrieve_connection
  end
end

これで動くようになる(^^ゞ!!

1
1
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
1
1