"PGError: no server connection" when running specs with Spork

I use Ruby 1.9.2, Rails 3.1, Rspec, Postgres and Spork, but I can't get them to play well together.

Fulfilling specifications for the first time (with Spork running in the background) works great. However, when I run the specs a second time, it fails with:

Failure/Error: Unable to find matching line from backtrace PGError: no connection to the server # /Users/tom/.rvm/gems/ ruby-1.9.2-p180@grapi /gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/postgresql_adapter.rb:272:in `exec' etc.... 

Any advice appreciated!

+4
source share
5 answers

You may also have Devise.

Your problem is described here: https://github.com/sporkrb/spork/wiki/Spork.trap_method-Jujutsu And more specifically for rails 3.1 here: https://gist.github.com/1054078

The start of your pre-sale block in spec_helper.rb and env.rb should look like this:

 Spork.prefork do Spork.trap_method(Rails::Application, :reload_routes!) Spork.trap_method(Rails::Application::RoutesReloader, :reload!) ... 

Good luck

+8
source

If you are using Factory Girl, do not use the factory_girl_rails gem, just use the factory_girl.

 Spork.each_run do FactoryGirl.definition_file_paths = [ File.join(Rails.root, 'spec', 'factories') ] FactoryGirl.find_definitions end 

For those using Factory Girl, Machinist or Shoulda Matchers, make sure you read about Spork trap_method at: https://github.com/timcharper/spork/wiki/Spork.trap_method-Jujutsu

He solved my problems with Spork and disconnected PostgreSQL connections during testing.

+3
source

you need to run spork --bootstrap

and after inserting some configuration into your spec_helper.rb file, so spork knows about your rail configuration.

As you use RSpec, you can try adding the following code to your spec_helper file:

 require 'rubygems' require 'spork' Spork.prefork do ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails' Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} RSpec.configure do |config| config.mock_with :rspec config.fixture_path = "#{::Rails.root}/spec/fixtures" config.use_transactional_fixtures = true # Needed for Spork ActiveSupport::Dependencies.clear end end Spork.each_run do load "#{Rails.root}/config/routes.rb" Dir["#{Rails.root}/app/**/*.rb"].each { |f| load f } end 
0
source

Could you try adding this to the Spork.each_run and see if it solves the problem?

 ActiveRecord::Base.connection_pool.verify_active_connections! 
0
source

I read the instructions https://github.com/timcharper/spork/wiki/Spork.trap_method-Jujutsu and found the following.

In my case, the solution was to change the way the driver downloads drawings. My pre-production block had the following line:

 Spork.prefork do ... require Rails.root.join 'spec/support/blueprints' ... 

I removed this from the prefork block and added this line to each_run instead:

 Spork.each_run do Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} ... 

Two lines basically do the same thing, so the main thing is that the seams do not load drawings in the presale, but rather in each_run.

Hope this helps!

0
source

All Articles