2011-07-17 24 views
4

我正在使用Ruby 1.9.2,Rails 3.1,Rspec,Postgres和Spork,但是我無法讓它們在一起很好地玩。使用Spork運行規格時出現「PGError:沒有連接到服務器」

第一次運行規格(Spork在後臺運行)工作正常。但是,當我第二次運行規格時,它失敗:

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

任何提示讚賞!

回答

8

您可能也啓用了Devise。

您的問題說明如下:https://github.com/sporkrb/spork/wiki/Spork.trap_method-Jujutsu 更具體鋼軌3.1位置:https://gist.github.com/1054078

您的prefork塊的開始spec_helper.rbenv.rb應該是這樣的:

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

好運!

+0

這解決了我的問題!太感謝了。 –

+0

這也給我修好了,謝謝 – tomblomfield

+1

工作過,但我也必須做FactoryGirl mod(下面)。 – Karl

0

你必須運行spork --bootstrap

和後插入一些配置,以你的spec_helper.rb文件,因此叉勺知道你的鐵軌配置。

當您使用RSpec的,你可以嘗試添加以下代碼到你的spec_helper文件:

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

我與OP有完全相同的問題。 spec_helper已正確引導。 –

+0

我的spec_helper看起來非常相似,依然沒有快樂。 – tomblomfield

+0

也許是rails 3.1的問題?我仍在3.0.9 – tmaximini

0

你能嘗試添加這Spork.each_run回調並檢查它是否解決了問題?

ActiveRecord::Base.connection_pool.verify_active_connections! 
+0

引發異常 遇到異常:# tomblomfield

0

我閱讀https://github.com/timcharper/spork/wiki/Spork.trap_method-Jujutsu上的說明並找到以下內容。

在我的情況下,解決方案是改變加工藍圖的加載​​方式。我的prefork的塊有這樣一行:

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

我刪除從prefork的塊,而是添加了此行each_run:

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

這兩條線基本上做同樣的事情,所以主要的接縫不要在prefork中加載藍圖,而是在each_run中加載藍圖。

希望它有幫助!

3

如果您使用Factory Girl,請勿使用'factory_girl_rails'gem,只需使用'factory_girl'。

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

對於使用工廠女孩,機械師,或早該匹配器的人,請確保您在閱讀叉勺的trap_method:https://github.com/timcharper/spork/wiki/Spork.trap_method-Jujutsu

它解決了我的問題,叉勺和測試時丟棄的PostgreSQL的連接。

+0

我也在使用FactoryGirl和黃瓜,並且爲'factory_girl_rails'換出香草'factory_girl'打破了我的功能。通過在'features/support/factory_girl.rb'中將文件添加到文件中來實現它 – chadoh

相關問題