0

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
Michael Cruz
  • 864
  • 6
  • 13
  • what does your `spec_helper` or `rails_helper` (if you are using RSpec 3) file contain? Might be caused by Database Cleaner – max Nov 05 '14 at 20:39
  • I have edited my post to include spec_helper.rb. Not sure how the DatabaseCleaner works, to be honest. Does it look like that's what's causing the issue? – Michael Cruz Nov 05 '14 at 20:59

1 Answers1

1

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
max
  • 96,212
  • 14
  • 104
  • 165
  • I gave this a try, and it did not solve the issue, but it did change the error message a bit to this: Failure/Error: Unable to find matching line from backtrace ActiveRecord::StatementInvalid: PG::ConnectionBad: connection is closed: BEGIN – Michael Cruz Nov 05 '14 at 21:45
  • Implementing this DOES, however, speed up my tests without using spork exponentially - as in from averaging ~ 25 seconds for one model spec to just over half a second. Thanks for that! For the time being, it makes my original problem much less annoying. – Michael Cruz Nov 05 '14 at 22:07
  • Did you see this question http://stackoverflow.com/questions/19828385/pgconnectionbad-could-not-connect-to-server-connection-refused ? – max Nov 07 '14 at 00:58
  • I have seen it, and the interesting thing is that I DID have a postmaster.pid in the postgresql data directory. After removing it, however, the problem persisted. – Michael Cruz Nov 07 '14 at 20:07