黄瓜 ActiveRecord::ConnectionNotEstablished

cucumber ActiveRecord::ConnectionNotEstablished

我遇到的异常是 "ActiveRecord::ConnectionNotEstablished: No connection pool for ActiveRecord::Base"。对于这个,我真的处于游泳池的深处(没有双关语)。连接和连接池的处理我是真的不懂,虽然我研究过这个问题。我假设这可能与 Cucumber 内部的范围相关,但我不知道。任何和所有帮助表示赞赏。

详情如下:

当我从 Then 子句执行计数时出现异常:

WorkTable.where('? is not null',col['COLUMN_NAME']).count

如果我直接通过连接发送 sql 就不会发生:

WorkTable.connection.select_all(st.encode('utf-8')).first['nulls']

我的场景如下:

  Scenario: CompanyMaster test for null value
    Given table dbo.base_table in stage
    Then these columns are expected to be not null
      | COLUMN_NAME                         | nulls |
      | id                                  | 0 |
      | company_name                        | 0 |

我在 env.rb:

中建立 class
class WorkTable < ActiveRecord::Base
end

ActiveRecord::Base.configurations = YAML.load_file(yaml)  # yaml is database.yml file name

我在 Given 子句中建立连接:

Given(/^table (\w+)\.?([\w_]+) in (\w+)(?: as (\w+))?$/) do |schema,name,env,id|
  @sc_name = schema_file_name(schema,name)
  WorkTable.logger.info title_line("* Active table(#{@sc_name}) *")
  case id
    #  ActiveRecord::Base.configurations[env]
    ...
    else
      WorkTable.table_name = @sc_name
      WorkTable.establish_connection(env.to_sym)
      # ary = get_tables(WorkTable,schema:schema)
      # expect( ary.any?{|s| s.casecmp(name)==0 } ).to eq(true)
  end
end

我在 Then 子句中执行我的测试:

Then(/^these columns are expected to be not null$/) do |columns|
  # expected is an instance of Cucumber::Ast::Table
  WorkTable.logger.info title_line('Columns cannot be null')

  results = []
  columns.hashes.each {|col|
    results << {
        'COLUMN_NAME' => col['COLUMN_NAME'],
        'nulls' => WorkTable.where('? is not null',col['COLUMN_NAME']).count.to_s
    }
  }
  columns.diff!(results,surplus_row: false)
end

抛出"ActiveRecord::ConnectionNotEstablished: No connection pool for ActiveRecord::Base"的是WorkTable.where。同样,如果我使用 WorkTable.connection 方法,我不会得到它。此外,如果我将所有 function 代码复制到单个 ruby 脚本,它也能正常执行。

我在 "pp WorkTable.connection" 时看到以下内容:

#<ActiveRecord::ConnectionAdapters::SQLServerAdapter version: 4.2.2, mode: dblib, azure: false>

我在 "pp WorkTable.connection_pool" 时看到以下内容:

#<ActiveRecord::ConnectionAdapters::ConnectionPool:0x42f5238
 @automatic_reconnect=true,
 @available=
  #<ActiveRecord::ConnectionAdapters::ConnectionPool::Queue:0x42f4f20
   @cond=
    #<MonitorMixin::ConditionVariable:0x42f4ed8
     @cond=
      #<ConditionVariable:0x42f4de8
       @waiters=[],
       @waiters_mutex=#<Mutex:0x42f4d58>>,
     @monitor=
      #<ActiveRecord::ConnectionAdapters::ConnectionPool:0x42f5238 ...>>,
   @lock=#<ActiveRecord::ConnectionAdapters::ConnectionPool:0x42f5238 ...>,
   @num_waiting=0,
   @queue=[]>,
 @checkout_timeout=5,
 @connections=
  [#<ActiveRecord::ConnectionAdapters::SQLServerAdapter version: 4.2.2, mode: dblib, azure: false>],
 @mon_count=0,
 @mon_mutex=#<Mutex:0x42f51c0>,
 @mon_owner=nil,
 @reaper=
  #<ActiveRecord::ConnectionAdapters::ConnectionPool::Reaper:0x42f51a8
   @frequency=nil,
   @pool=#<ActiveRecord::ConnectionAdapters::ConnectionPool:0x42f5238 ...>>,
 @reserved_connections=
  #<ThreadSafe::Cache:0x42f4fc8
   @backend=
    {16931712=>
      #<ActiveRecord::ConnectionAdapters::SQLServerAdapter version: 4.2.2, mode: dblib, azure: false>},
   @default_proc=nil>,
 @size=5,
 @spec=
  #<ActiveRecord::ConnectionAdapters::ConnectionSpecification:0x42f55c8
   @adapter_method="sqlserver_connection",
   @config=
    {:host=>"server_name",
     :database=>"mssb_stg",
     :encoding=>"utf-8",
     :adapter=>"sqlserver",
     :timeout=>5000}>>

Ruby 1.9.3、activerecord (4.2.0)、activerecord-sql服务器适配器 (4.2.2) 和黄瓜 (1.3.18)。 sql 服务器 2014 [这对我来说一直是个问题]。

感谢您的时间和考虑。 dvn

== 更多细节 ==

忽略 sql-服务器引用。当我重新配置以使用 SqLite 时,我遇到了同样的异常。所以和db平台无关。

检查你的env.rb,conf in supports,看起来你正在逐步建立连接,理想情况下你应该在before_scenariobefore feature文件中而不是按步骤进行。

您的连接可能在执行步骤后无法正常工作。