我正在使用mochaoose的Mocha。测试运行速度非常慢,需要很长时间才能执行,有时长达8秒。我连接到我系统上的本地mongo数据库。 相关文件如下:
test_helper.js
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
//before executed only once for the entire test suite
before((done)=>{
mongoose.connect('mongodb://localhost:20000/users_test');
mongoose.connection
.once('open',()=> {done();})
.on('error',(error)=>{
console.warn('Warning',error);
});
});
//define a hook - a function that gets executed before any test runs
beforeEach((done)=>{
mongoose.connection.collections.users.drop(()=>{
//ready to run the next test!
done();
});
});

user.js文件
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const UserSchema = new Schema({
name:String,
postCount:Number
});
const User = mongoose.model('user', UserSchema);
module.exports = User;

测试分布在几个文件中。我已将文件上传到驱动器上,链接是: update_test.js - https://drive.google.com/open?id=1fHO8gLrRE3QsRfRfbwbpOOEg0zcGGxPL
reading_test.js - https://drive.google.com/open?id=1Kck1GS8j07MCQyYf0a1xpiGJ3lvvowLF
delete_test.js - https://drive.google.com/open?id=1KLt61139ey4BXg0HC9p8V3_Q5fH0UP39
create_test.js - https://drive.google.com/open?id=1AN2d2gq70UnNky5_4GQ6_fKlxj28WEA-