我正在为mongodb周围的操作编写节点单元测试。当我使用nodeunit (nodeunit testname.js)执行测试时,测试通过并变为绿色,但nodeunit命令行不返回(我需要按ctrl-c)。
我做错了什么?我是否需要关闭我的数据库连接或服务器,或者我的测试出错了?
这是一个简化的样本测试。
process.env.NODE_ENV = 'test';
var testCase = require('/usr/local/share/npm/lib/node_modules/nodeunit').testCase;
exports.groupOne = testCase({
tearDown: function groupOneTearDown(cb) {
var mongo = require('mongodb'), DBServer = mongo.Server, Db = mongo.Db;
var dbServer = new DBServer('localhost', 27017, {auto_reconnect: true});
var db = new Db('myDB', dbServer, {safe:false});
db.collection('myCollection', function(err, collectionitems) {
collectionitems.remove({Id:'test'}); //cleanup any test objects
});
cb();
},
aTest: function(Assert){
Assert.strictEqual(true,true,'all is well');
Assert.done();
}
});迈克尔
发布于 2012-12-21 09:11:36
尝试在关闭连接后将您的cb()放入remove()回调中:
var db = new Db('myDB', dbServer, {safe:false});
db.collection('myCollection', function(err, collectionitems) {
collectionitems.remove({Id:'test'}, function(err, num) {
db.close();
cb();
});
}); 发布于 2014-09-10 15:55:34
您需要在关闭db之后(在tearDown期间)调用cb函数:
tearDown: function(cb) {
// ...
// connection code
// ...
db.collection('myCollection', function(err, collectionitems) {
// upon cleanup of all test objects
db.close(cb);
});
}这对我很有效。
https://stackoverflow.com/questions/13980956
复制相似问题