Not connecting to PG when running Spork

214 Views Asked by At

I have been trying to find an answer to this on and off, but reading similar SO posts hasn't solved my issue yet. I have a rails app using rspec and spork. If I just run rspec without spork, my tests run just fine, but they are very slow. When I use spork, however, I get the following error:

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

Here is (most) of my 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

...and my database.yml (dev and test):

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

NOTES: In many other similar posts, people were only getting a similar error on their second and subsequent times running tests, but in my case, the error comes on the first run AND all others. No problems connecting to PG in dev mode using rails s.

EDIT I tried upgrading my bundle to solve the issue (thinking maybe it was an issue solved by a later version of rspec, guard or spork, but to no avail. I did, however, make the necessary changes to spork.rb listed at https://github.com/guard/guard/wiki/Upgrading-to-Guard-2.0#changes-in-guardguard and I also updated rspec.rb as suggested here: https://github.com/manafire/spork/commit/38c79dcedb246daacbadb9f18d09f50cc837de51#diff-937afaa19ccfee172d722a05112a7c6fL6, which solved the problems related to updating the gems, but did not solve my original problem.

EDIT TO INCLUDE SPEC_HELPER Here is my spec_helper.rb file:

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
1

There are 1 best solutions below

4
max On

Not completely sure this will solve your issue but using transactions instead of truncating is a lot faster:

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