2011-07-17 29 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

相关问题