PG::ConnectionBad:从 rails 从 4.2 升级到 5.2 后连接关闭

PG::ConnectionBad: connection is closed after upgrading rails from 4.2 to 5.2

我已经将我的 rails 项目版本从 4.2 升级到 5.2.1 我的一些测试失败了,因为完整跟踪上的 pg 连接关闭错误如下。

    Failure/Error: DatabaseCleaner[:active_record].clean_with(:truncation)

    ActiveRecord::StatementInvalid:
    PG::ConnectionBad: connection is closed: TRUNCATE TABLE "public"."alerts", "public"."article_attachments", "public"."article_check_specification_tolerances", "public"."article_machine_part_settings", "public"."articles", "public"."attachments", "public"."check_batches", "public"."check_groups", "public"."check_specification_machine_types", "public"."check_specification_priorities", "public"."check_specification_responsibility_areas", "public"."check_specifications", "public"."checks", "public"."comments", "public"."counters", "public"."customers", "public"."defect_groups", "public"."defect_translations", "public"."defects", "public"."delayed_jobs", "public"."furnaces", "public"."gv_area_equipments", "public"."gv_areas", "public"."gv_components", "public"."gv_entries", "public"."gv_equipment_families", "public"."gv_equipments", "public"."gv_squads", "public"."gv_stop_reasons", "public"."gv_sub_equipment_components", "public"."gv_sub_equipments", "public"."job_specifications", "public"."jobs", "public"."lab_recipe_versions", "public"."lab_recipes", "public"."lines", "public"."machine_downtimes", "public"."machine_groups", "public"."machine_part_change_reasons", "public"."machine_part_changes", "public"."machine_part_translations", "public"."machine_parts", "public"."machine_type_group_machine_types", "public"."machine_type_groups", "public"."machine_types", "public"."messages", "public"."mold_sets", "public"."packing_schemes", "public"."rails_admin_settings", "public"."reasons", "public"."rejects", "public"."responsibility_areas", "public"."roles", "public"."settings", "public"."shift_definitions", "public"."shifts", "public"."system_log_entries", "public"."task_status_changes", "public"."tasks", "public"."tresholds", "public"."user_responsibility_areas", "public"."users", "public"."users_roles", "public"."workstations", "public"."machines", "public"."systematic_rejects", "public"."systematic_reject_machines" RESTART IDENTITY CASCADE;

我的 rspec 配置是

RSpec.configure do |config|
  config.around(:each) do |example|
    DatabaseCleaner[:active_record].clean_with(:truncation)
    # DatabaseCleaner.clean_with(:truncation)
    DatabaseCleaner.cleaning do
      example.run
    end
  end
end

奇怪的是我可以运行像

一样一个一个地测试目录
rspec spec/controllers/
rspec spec/contexts/
rspec spec/models

但是当我尝试 运行 与 just

合而为一时它不起作用
rspec spec/featues/

我的feature_helper.rb是

require 'spec_helper'
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
require 'capybara/rails'
require 'rack/handler/puma'
require 'support/shared_activerecord_connection'
require 'support/feature_macros'

Capybara.register_server :puma do |app, port, host|
  require 'rack/handler/puma'
  Capybara.asset_host = "http://#{host}:#{port}"
  Rack::Handler::Puma.run(app, Host: host, Port: port, Threads: '0:4', Silent: true, config_files: ['-'])
end
Capybara.configure do |config|
  config.server = :puma
end

RSpec.configure do |config|
  config.before(:suite) do
    Rails.application.load_tasks
    Rake::Task['assets:precompile'].invoke
  end
end

require 'spec_helper'
require 'counter_column_fix'

RedisStore.class_eval do
  def self.new_instance
    $redis = Redis.new(Rails.configuration.redis_config)
  end
end

我应该怎么做才能消除这个错误?

DatabaseCleaner 通常不需要 Rails 5.1+。从您的项目中删除对它的所有引用,并在您的 RSpec 配置

中启用事务测试