【问题标题】:Using after to clean up in mocha, problems with mongo在 mocha 中使用 after 清理,mongo 的问题
【发布时间】:2015-08-23 05:00:42
【问题描述】:

好的,在console.log "enter after function"console.log "exit"之后的函数中执行,但是console.log "WHYYYY"没有执行。我不知道为什么。作为记录,console.log 'insert' 确实执行了。我在我的一个测试中对 mongo 进行了类似的调用,所以调用 mongo 应该可以工作。

关于这个问题的任何线索或如何解决它?

describe 'HTTP Requests', ->
    describe 'Socky', ->
        socky = new Socky config
        socky.listen()

        before ->
            mongo.open (error, db) =>
                secondDb = db.db config.api.accountTest
                secondDb.collection 'accounts', (error, accountsColl) =>
                    accountsColl.insert {'_id': ObjectID("#{ accountId }"), 'name': "test-account"}, (error, records) =>
                        console.log 'insert'
                        db.close()
                        return

                db.collection 'tokens', (error, tokensColl) =>
                    tokensColl.insert {'accountId': "#{ accountId }", 'userId': "#{ userId }", 'token': 'testtoken'}, (error, records) =>
                        null
                        db.close()
                    return
                db.close()
                return

        after ->
            console.log "enter after function"

            # console.dir mongo
            mongo.open (error, db) =>
                console.log "WHYYYY"
                secondDb = db.db config.api.accountTest
                console.log error


                secondDb.collection 'accounts', (error, accountsColl) =>
                    accountsColl.remove {}, (error, records) =>
                        console.log "remove?"
                        null
                        return

                db.collection 'tokens', (error, tokensColl) =>
                    tokensColl.remove {}, (error, records) =>
                        # db.close()
                        return
                return
            console.log "exit after"

【问题讨论】:

    标签: node.js mongodb coffeescript mocha.js


    【解决方案1】:

    Mongo 没有足够的时间连接,

    尝试在 after 函数中使用 done 回调:

    after (done) ->
      console.log "WHYYYY"
      mongo.open (error, db) =>
         ...
         done()
      console.log "exit after"
    })
    

    【讨论】:

      猜你喜欢
      • 2014-06-17
      • 2021-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-19
      • 2022-11-24
      • 1970-01-01
      • 2021-07-08
      相关资源
      最近更新 更多