gpt4 book ai didi

node.js - 为什么我的 mongodb nodeunit 测试没有完成?

转载 作者:可可西里 更新时间:2023-11-01 09:46:00 25 4
gpt4 key购买 nike

我正在为围绕 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();
}
});

迈克尔

最佳答案

尝试在关闭连接后将 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();
});
});

关于node.js - 为什么我的 mongodb nodeunit 测试没有完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13980956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com