4

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

Running the specs for the first time (with Spork running in the background) works fine. 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 tips appreciated!

5 Answers 5

8

You probably also have Devise enabled.

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 beginning of your prefork block in spec_helper.rb and env.rb should looks like:

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

Good luck!

3
  • 1
    Worked, but I had to do the FactoryGirl mod (below) as well.
    – Karl
    Sep 2, 2011 at 21:21
  • 1
    I'm not using Devise at all, but it still fixed it for me. I am also using Shoulda, so I had to add require 'shoulda/integrations/rspec2' after require 'rspec/rails'
    – mindtonic
    Sep 30, 2011 at 15:16
  • In order to get this to work when running rake spec, I had to add require "rails/application" before the first trap_method call. Thanks!
    – chadoh
    Oct 26, 2011 at 0:18
3

If you're using Factory Girl, don't use the 'factory_girl_rails' gem, just use 'factory_girl'.

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

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

It solved my problems with Spork and the dropped PostgreSQL connections while testing.

1
  • I'm also using FactoryGirl with Cucumber, and swapping out vanilla 'factory_girl' for 'factory_girl_rails' broke my features. Got it to work by adding what you have there to a file in features/support/factory_girl.rb
    – chadoh
    Oct 27, 2011 at 0:45
0

you have to run spork --bootstrap

and after insert some configuration to your spec_helper.rb file, so spork knows about your rails 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
3
  • I'm having the exact same problem as the OP. spec_helper is properly bootstrapped. Jul 20, 2011 at 11:20
  • My spec_helper looks very similar to that, still no joy. Jul 20, 2011 at 16:54
  • maybe a rails 3.1 problem? I am still on 3.0.9
    – tmaximini
    Aug 4, 2011 at 11:41
0

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

ActiveRecord::Base.connection_pool.verify_active_connections!
1
  • That throws the exception Exception encountered: #<PGError: no connection to the server> Jul 20, 2011 at 17:11
0

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

In my case the solution was to change how machinist blueprints was loaded. My prefork block had this line:

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

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

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

The two lines basically does the same thing, so the main thing seams to be not to load the blueprints in the prefork, but rather in each_run.

Hope it helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.