1

我有多个带有单元测试的文件。我想避免连接它们。我需要关闭猫鼬连接gulp-jasmine才能退出。我还想避免将连接处理放入一个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()

编辑:

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

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

错误

{ Error: Trying to open unclosed connection.

问题

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

4

1 回答 1

0

这似乎有效,仍然想知道为什么我不能使用该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
于 2016-09-23T08:49:11.850 回答