【问题标题】:cucumber ActiveRecord::ConnectionNotEstablished黄瓜 ActiveRecord::ConnectionNotEstablished
【发布时间】:2015-03-28 16:09:49
【问题描述】:

我得到的例外是“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: No connection pool for ActiveRecord::Base”。同样,如果我使用 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-sqlserver-adapter (4.2.2) 和 cucumber (1.3.18)。还有 sql server 2014 [这对我来说是个麻烦]。

感谢您的时间和考虑。 dvn

== 更多细节 ==

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

【问题讨论】:

    标签: activerecord cucumber


    【解决方案1】:

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

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

    【讨论】:

    • 我逐步建立连接有两个主要原因:a) 我认为这是正确的位置,b) 在这种情况下它是正确的位置。当我在 Given 中声明“处于阶段”时,它是“阶段”,它标识了我要连接到的 database.yml 环境。这可能会因场景而异。所以我在步骤中这样做。谢谢(你的)信息。我以前从未考虑过钩子,但这会在其他应用程序中清理干净。
    猜你喜欢
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多