【问题标题】:Jasmine Open And Close Db ConnectionsJasmine 打开和关闭数据库连接
【发布时间】:2017-02-01 00:09:24
【问题描述】:

我有多个包含单元测试的文件。我想避免连接它们。我需要关闭猫鼬连接才能将gulp-jasmine 连接到exit。我还想避免将连接处理放入it 块中,因为它不属于那里。

如果我将连接/断开功能移动到 beforeAllafterAll 例如

代码

单元测试

describe "a unit test"
  beforeAll (done)->
    db = testSettings.connectDB (err)->
      throw err if err?
      done()

  ...

  afterAll (done)->
    testSettings.disconnectDB (err)->
      throw err if err?
      done()

然后Jasmine 执行下一个描述beforeAll 之前afterAll 可以正确断开数据库。

(茉莉花文档)[http://jasmine.github.io/2.1/introduction.html#section-Setup_and_Teardown]

但是,使用 beforeAll 和 afterAll 时要小心!既然他们不是 在规格之间重置,很容易在规格之间意外泄漏状态 您的规格,以便它们错误地通过或失败。

连接功能

connections = 0

exports.connectDB = (callback) ->
  configDB = require(applicationDir + 'backend/config/database.js')(environment)

  if connections == 0
    mongoose.connect configDB.url,{auth:{authdb:configDB.authdb}}, (err)->
      if (err)
        console.log(err)
        return callback err

    db = mongoose.connection
    db.on 'error', console.error.bind(console, 'connection error:')

    db.once 'open', () ->
      connections++
      return callback null, db
      #console.log "Database established"
      #Delete all data and seed new data

  else
    db = mongoose.connection
    return callback null, db

exports.disconnectDB = (callback) ->
  mongoose.disconnect (err)->
    return callback err if err?
    connections--
    return callback()

编辑:

监听断开事件也不起作用: export.disconnectDB =(回调)-> console.log "DISCONNECTING FROM DB", mongoose.connection.readyState mongoose.disconnect (err)-> 错误返回回调错误? 连接—— console.log "应该断开连接", mongoose.connection.readyState #不是因为状态是 3 0> 断开连接 返回回调()

mongoose.connection '断开', () -> console.log "DIS", mongoose.connection.readyState 返回回调()

错误

{ Error: Trying to open unclosed connection.

问题

如何正确打开和关闭我与gulp-jasmine 的连接?

【问题讨论】:

    标签: node.js unit-testing jasmine gulp-jasmine


    【解决方案1】:

    这似乎行得通,仍然想知道为什么我不能使用disconnected 事件:

    connections = 0
    disConnectionRetries = 0
    MAX_DISCONNECTION_RETRIES = 10
    
    connectDB = (callback) ->
      console.log "CONNECTING TO DB", mongoose.connection.connectionState
      configDB = require(applicationDir + 'backend/config/database.js')(environment)
    
      if connections == 0
        mongoose.connect configDB.url,{auth:{authdb:configDB.authdb}}, (err)->
          if (err)
            console.log(err)
            return callback err
    
        db = mongoose.connection
        db.on 'error', console.error.bind(console, 'connection error:')
    
        db.once 'open', () ->
          connectionRetries = 0
          connections++
          return callback null, db
          #console.log "Database established"
          #Delete all data and seed new data
    
      else
        db = mongoose.connection
        return callback null, db
    
    exports.disconnectDB = (callback) ->
      console.log "DISCONNECTING FROM DB", mongoose.connection.readyState
      mongoose.disconnect (err)->
        return callback err if err?
        connections--
        isDisconnected(callback)
    
    isDisconnected = (callback)->
      console.log "SHOULD BE DISCONNETCED", mongoose.connection.readyState
      throw new Error "Cannot disconnect more than #{MAX_DISCONNECTION_RETRIES} retries" if disConnectionRetries > MAX_DISCONNECTION_RETRIES
      if mongoose.connection.readyState == 0
        disConnectionRetries = 0
        return callback()
      else
        disConnectionRetries++
        return setTimeout ()->
          isDisconnected(callback)
        , 50
    

    【讨论】:

      猜你喜欢
      • 2019-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-03
      • 2010-11-19
      相关资源
      最近更新 更多