黄瓜ActiveRecord :: ConnectionNotEstablished

时间:2015-03-27 12:08:23

标签: activerecord cucumber

我得到的异常是“ActiveRecord :: ConnectionNotEstablished: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 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

WorkTable.where抛出“ActiveRecord :: ConnectionNotEstablished:ActiveRecord :: Base没有连接池”。再次,如果我使用WorkTable.connection方法,我不明白。此外,如果我将所有函数代码复制到单个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-sqlserver-adapter(4.2.2)和cucumber(1.3.18)。和sql server 2014 [这对我来说是一个bug]。

谢谢你的时间和考虑。 DVN

==其他详细信息==

忽略sql-server引用。当我重新配置使用SqLite时,我得到了相同的异常。所以它与db平台无关。

1 个答案:

答案 0 :(得分:0)

检查你的env.rb,conf支持,似乎你正在逐步建立联系,理想情况下你应该在before_scenariobefore feature文件中而不是按步骤进行。

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

相关问题