Nodeunittesting挂起asynchronous数据库调用ORMnomnom

我正在尝试为asynchronous数据库调用编写unit testing。 我使用ORMnomnom包作为orm数据库访问和nodeunit进行unit testing的NodeJS 。 但是这个简单的testing挂起来了:

这里是代码test \ test1.js

modelsdb = require('../model/model_db') exports['test db'] = function (test) { test.expect(1); console.log('test db'); modelsdb.MyTable.objects.all( function (err, data) { test.ok(true, "this assertion should pass"); test.done(); console.log('test must finish here!'); }); } exports.testSomething = function(test){ console.log('testSomething'); test.expect(1); test.ok(true, "this assertion should pass"); test.done(); }; 

当我运行这个testing所有的断言通过,我看到我的消息在控制台:'testing数据库''testing必须在这里完成! 'testSomething'(意思是test.done()到达callback函数内部),但是testing没有完成。 我需要手动停止它,得到:'处理完成退出代码1'。 如果我更改为test.ok(false,“”),所以我得到AssertionError但testing没有完成。 如果我删除“testing数据库”,只留下testSomething函数 – testing完成如预期,所有断言通过。

我也尝试基于nodeunit的 testpilot软件包。 它给

 test1 FAIL : test db an error occurred during test: Error: timed out waiting for test 

有什么build议么? 谢谢。

tearDown方法添加到导出来closuresmongoose的连接池。 只要它仍然打开,testing不会退出。

你不显示你如何打开你的mongoose的连接,但是这样的事情:

 exports.tearDown = function (callback) { mongoose.disconnect(callback); }; 

我有一个与nodeunit和q / mongoose类似的问题 – 有一个已经打开它的错误 。 我试过在调用test.done() process.exit(0)之后做可怕的process.exit(0) ,但是任何挂起的日志都可能不会被刷新,所以它不是很理想。 结束不得不在超时块中调用process.exit(0)