gpt4 book ai didi

node.js - 在 Mocha 测试中,一半的时间都不会创建 Mongoose 索引

转载 作者:太空宇宙 更新时间:2023-11-03 23:33:30 25 4
gpt4 key购买 nike

每当我运行 Mocha 测试时,它都会在创建索引和不创建索引之间交替。我认为它以某种方式没有创建索引,因为测试可能在完成之前就已经运行了,但由于它以这样的模式交替,我认为情况并非如此。我还认为这可能与我在每次测试开始时删除数据库有关,但这不应以某种方式仅影响所有其他测试。

相关索引:

submissionSchema.index({ studentID: 1, assignmentID: 1 }, { unique: true });

删除数据库的代码:

before(function(done){
mongoose.createConnection(require(__dirname + '/../app/config').mongoURL, function(err){
if (err) throw err;
mongoose.connection.db.dropDatabase(function(err){
if (err) throw err;
done();
});
});
});

知道是什么原因造成的吗?

最佳答案

布莱克·塞文斯是对的。为了解决这个问题,我只是在删除数据库后重建了索引。

before(function(done){
mongoose.createConnection(require(__dirname + '/../app/config').mongoURL, function(err){
if (err) throw err;
mongoose.connection.db.dropDatabase(function(err){
if (err) throw err;

var rebuildIndexes = []

var models = Object.keys(mongoose.connections[0].base.modelSchemas);

models.forEach(function(model){
rebuildIndexes.push(function(cb){
mongoose.model(model, mongoose.connections[0].base.modelSchemas[model]).ensureIndexes(function(err){
return cb(err);
})
});
});

async.parallel(rebuildIndexes, function(err) {
if (err) throw err;
console.log('Dumped database and restored indexes');
done();
});
});
});
});

关于node.js - 在 Mocha 测试中,一半的时间都不会创建 Mongoose 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35388957/

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