我一直试图找到这个问题的答案,但阅读类似的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,警卫或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,它解决了与更新宝石有关的问题,但没有解决我原来的问题。 / p>
编辑包含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
答案 0 :(得分: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