在 mocha 中使用 after 进行清理,mongo 出现问题
Using after to clean up in mocha, problems with mongo
ok,在after函数中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"
Mongo没有足够的时间连接,
尝试在 after 函数中使用 done 回调:
after (done) ->
console.log "WHYYYY"
mongo.open (error, db) =>
...
done()
console.log "exit after"
})
ok,在after函数中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"
Mongo没有足够的时间连接,
尝试在 after 函数中使用 done 回调:
after (done) ->
console.log "WHYYYY"
mongo.open (error, db) =>
...
done()
console.log "exit after"
})