【问题标题】:Not connecting to PG when running Spork运行 Spork 时未连接到 PG
【发布时间】:2014-11-05 19:15:21
【问题描述】:

我一直试图断断续续地寻找答案,但阅读类似的 SO 帖子还没有解决我的问题。我有一个使用 rspec 和 spork 的 rails 应用程序。如果我只运行没有 spork 的 rspec,我的测试运行得很好,但它们非常慢。但是,当我使用 spork 时,出现以下错误:

ActiveRecord::StatementInvalid:
   PG::ConnectionBad: connection is closed:               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                        pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
                   FROM pg_attribute a LEFT JOIN pg_attrdef d
                     ON a.attrelid = d.adrelid AND a.attnum = d.adnum
                  WHERE a.attrelid = '"orders"'::regclass
                    AND a.attnum > 0 AND NOT a.attisdropped
                  ORDER BY a.attnum

这是我的(大部分)gemfile:

gem 'rails', '4.1.5'
gem 'bootstrap-sass', '2.3.2.0'
gem 'sprockets'
gem 'minitest'
gem 'bcrypt-ruby'
gem 'faker', '1.1.2'
gem 'will_paginate', '3.0.4'
gem 'bootstrap-will_paginate', '0.0.9'
gem 'pg'
gem 'cocoon'

group :development, :test do
  gem 'rspec-rails'
  gem 'guard-rspec'
  gem 'spork-rails'
  gem 'guard-spork'
  gem 'childprocess'
  gem 'database_cleaner'
end

group :test do
  gem 'selenium-webdriver'
  gem 'capybara'
  gem 'factory_girl_rails'
end

...和我的 database.yml(开发和测试):

development:
  adapter: postgresql
  host: localhost
  username: user
  password: 'password'
  database: test_app_development

test:
  adapter: postgresql
  host: localhost
  username: user
  password: 'password'
  database: test_app_test

注意:在许多其他类似的帖子中,人们只在第二次和后续运行测试时遇到类似错误,但在我的情况下,错误出现在第一次运行和所有其他情况下。使用 rails s 在开发模式下连接到 PG 没有问题。

编辑我尝试升级我的包来解决这个问题(认为这可能是由更高版本的 rspec、guard 或 spork 解决的问题,但无济于事。但是,我做了在https://github.com/guard/guard/wiki/Upgrading-to-Guard-2.0#changes-in-guardguard 中列出了对 spork.rb 的必要更改,我还按照此处的建议更新了 rspec.rb:https://github.com/manafire/spork/commit/38c79dcedb246daacbadb9f18d09f50cc837de51#diff-937afaa19ccfee172d722a05112a7c6fL6,这解决了与更新 gems 相关的问题,但没有解决我原来的问题。

编辑以包含 SPEC_HELPER 这是我的 spec_helper.rb 文件:

require 'rubygems'
require 'spork'

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}

  ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

  RSpec.configure do |config|

    config.use_transactional_fixtures = false

    config.before(:suite) do
      DatabaseCleaner.strategy = :truncation
    end

    config.before(:each) do
      DatabaseCleaner.start
    end

    config.after(:each) do
      DatabaseCleaner.clean
    end

    config.fixture_path = "#{::Rails.root}/spec/fixtures"

    config.infer_base_class_for_anonymous_controllers = false

    config.order = "random"
    config.include Capybara::DSL
  end
end

Spork.each_run do
end

【问题讨论】:

  • 您的spec_helperrails_helper(如果您使用的是RSpec 3)文件包含什么?可能是由数据库清理程序引起的
  • 我已经编辑了我的帖子以包含 spec_helper.rb。老实说,不确定 DatabaseCleaner 的工作原理。看起来这就是导致问题的原因吗?

标签: ruby-on-rails postgresql rspec spork


【解决方案1】:

不完全确定这会解决您的问题,但使用事务而不是截断要快得多:

config.before(:each) do
  # Use really fast transaction strategy for all
  # examples except `js: true` capybara specs
  DatabaseCleaner.strategy = example.metadata[:js] ? :truncation : :transaction
  DatabaseCleaner.start
end

【讨论】:

  • 我试了一下,并没有解决问题,但它确实将错误消息更改为:Failure/Error: Unable to find matching line from backtrace ActiveRecord::StatementInvalid: PG::ConnectionBad:连接已关闭:BEGIN
  • 然而,实施这个确实可以在不使用 spork 的情况下以指数方式加速我的测试 - 从一个模型规格的平均约 25 秒到半秒多一点。感谢那!暂时,它让我原来的问题不那么烦人了。
  • 你看到这个问题stackoverflow.com/questions/19828385/…了吗?
  • 我看到了,有趣的是我确实在 postgresql 数据目录中有一个 postmaster.pid。但是,删除它后,问题仍然存在。
猜你喜欢
  • 2016-08-03
  • 1970-01-01
  • 2013-03-30
  • 2021-08-15
  • 2016-09-18
  • 1970-01-01
  • 2018-07-18
  • 1970-01-01
  • 2013-11-05
相关资源
最近更新 更多