挂钩之前的 Mocha js 和 require 不能正常执行
Mocha js before hook and require not executing properly
我正在为一个项目编写单元测试,数据库模块是独立的(它建立与数据库的连接并具有批量插入方法)。我的单元测试文件中的代码如下:
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 default connection open to DB-URI
接着是
We are in the before hook
Inserting 1800 datapoints into database
Inserting 1800 datapoints into database [10 times]
但我明白了
We are in the before hook
Inserting 1800 datapoints into database
Inserting 1800 datapoints into database [10 times]
其次是
Mongoose default connection open to: DB_URI
我做了一些搜索,发现要求应该是同步的,mocha 单元测试也是如此。我在这里错过了什么?你能告诉我发生了什么事吗?
May be the connection in the db.js is lazy? –
懒惰的意思是,如果在您的 db.js 文件中有这样的内容:
mongoose.connect('mongodb://localhost/stack');
这不是同步的,只是一个挂起的连接。可以抓取连接监听open事件:
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
// we're connected!
});
在此事件触发之前,连接尚未就绪。
另一方面,mocha 可以处理同步和异步代码。但是因为你有一个回调,你的代码是异步的,所以你必须添加 done 回调。
describe('User', function() {
describe('#save()', function() {
it('should save without error', function(done) {
var user = new User('Luna');
user.save(function(err) {
if (err) throw err;
done();
});
});
});
});
在这种情况下,done 回调位于 it 部分,但它对所有挂钩的作用相同。
我正在为一个项目编写单元测试,数据库模块是独立的(它建立与数据库的连接并具有批量插入方法)。我的单元测试文件中的代码如下:
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 default connection open to DB-URI
接着是
We are in the before hook
Inserting 1800 datapoints into database
Inserting 1800 datapoints into database [10 times]
但我明白了
We are in the before hook
Inserting 1800 datapoints into database
Inserting 1800 datapoints into database [10 times]
其次是
Mongoose default connection open to: DB_URI
我做了一些搜索,发现要求应该是同步的,mocha 单元测试也是如此。我在这里错过了什么?你能告诉我发生了什么事吗?
May be the connection in the db.js is lazy? –
懒惰的意思是,如果在您的 db.js 文件中有这样的内容:
mongoose.connect('mongodb://localhost/stack');
这不是同步的,只是一个挂起的连接。可以抓取连接监听open事件:
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
// we're connected!
});
在此事件触发之前,连接尚未就绪。
另一方面,mocha 可以处理同步和异步代码。但是因为你有一个回调,你的代码是异步的,所以你必须添加 done 回调。
describe('User', function() {
describe('#save()', function() {
it('should save without error', function(done) {
var user = new User('Luna');
user.save(function(err) {
if (err) throw err;
done();
});
});
});
});
在这种情况下,done 回调位于 it 部分,但它对所有挂钩的作用相同。