3

我正在为围绕 mongodb 的操作编写 nodeunit 测试。当我使用 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();
}
});

迈克尔

4

2 回答 2

2

关闭连接后,尝试将您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();
    });
}); 
于 2012-12-21T01:11:35.737 回答
0

You need to invoke cb function after the closure of db (during the tearDown):

tearDown: function(cb) {

    // ...
    // connection code
    // ...

    db.collection('myCollection', function(err, collectionitems) {
        // upon cleanup of all test objects
        db.close(cb);
    });
}

This works for me.

于 2014-09-10T07:55:33.873 回答