【发布时间】:2016-07-16 06:24:52
【问题描述】:
我正在为一个项目编写单元测试,数据库模块是独立的(它建立与数据库的连接并具有批量插入方法)。我的单元测试文件中的代码如下:
var database = require('./db.js'); //once the database is connected there is a log saying connected to the database
var Data = database.model; //module export for the model
before(function(){
console.log("We are in the before hook");
for(var i = 1; i <= 10; i++){
var startDate = new Date(2016,1,1,0,0,0,0);
var endDate = new Date(2016,1,1,1,0,0,0);
var data = test_data.genIncreasing('Watch_'+i , startDate.getTime(), endDate.getTime() , 2000, 9); //will get around 3600 points
console.log('Inserting ' + data.length + ' datapoints into database');
database.bulkInsert(data, function(err, data){
if(err){
Should.fail('Could not insert data into data base');
}else{
console.log('Inserted Watch_' + i + ' data into database');
}
});
}
});
现在我期待在我的控制台中看到
Mongoose 默认连接打开到 DB-URI
紧随其后
我们在前面的钩子里
将 1800 个数据点插入数据库
将 1800 个数据点插入数据库 [10 次]
但我明白了
我们在前面的钩子里
将 1800 个数据点插入数据库
将 1800 个数据点插入数据库 [10 次]
紧随其后
Mongoose 默认连接打开到:DB_URI
我做了一些搜索,发现要求是同步的,mocha 单元测试也是如此。我在这里想念什么?你能告诉我正在发生的事情吗?
【问题讨论】:
-
我假设你之前在
describe? -
不,它在外面,但是当我把它放在描述中时,我得到了相同的结果
-
可能是
db.js中的连接是懒惰的? -
对不起,你说 db.js 很懒是什么意思?
标签: node.js mongodb unit-testing mocha.js