我正在尝试使用SimpleDbLayer 为我的简单数据库图层nodeunit 编写一组测试单元。我发现它很难。
问题:
1)我想连接到DB一次,并使用该连接。我不能真正使用setUp和tearDown,因为它们在每个测试之前和之后运行。作为“解决方案”,我在范围内使用模块范围的变量(参见下面的代码)
2)我的简单DB层有几个DB驱动程序。 MongoMixin只是其中之一 - 我正在开发MariaMixin和PostgresMixin。理想情况下,我会为每个驱动程序运行一次这些测试,看看它们是否有效。有一个好的,简单的方法吗?
3)我遇到了我的测试代码出错的情况,我得到的是关于test.done未被发现的投诉。我需要注意哪些诡计?
4)我做了什么可怕的错误吗? (见代码)var
dummy
, declare = require('simpledeclare')
, SimpleDbLayer = require('./SimpleDbLayer')
, MongoMixin = require('./MongoMixin.js')
, mw = require('mongowrapper')
, async = require('async')
;
var db, layer;
exports.createLayer = {
databaseConnect: function( test ){
mw.connect('mongodb://localhost/hotplate', {}, function( err, returnedDb ){
test.ifError( err );
db = returnedDb;
test.done( null );
});
},
makeLayer: function( test ){
var C = declare( [ SimpleDbLayer, MongoMixin ] );
layer = new C( 'test', { name: true, surname: true, age: true }, db );
test.ok( layer );
test.done();
},
deleteAll: function( test ){
layer.delete( { }, { multi: true }, function( err, howmany ){
test.ifError( err );
test.done();
});
},
insert: function( test ){
var people = [
{ name: 'Chiara', surname: 'Mobily', age: 24 },
{ name: 'Tony', surname: 'Mobily', age: 37 },
{ name: 'Sara', surname: 'Connor', age: 14 },
{ name: 'Daniela', surname: 'Mobily', age: 64 },
];
returnedPeople = [];
var functions = [];
// Populate the database
people.forEach( function( person ){
functions.push( function( done ){
layer.insert( person, { returnRecord: true }, function( err, person ){
test.ifError( err );
returnedPeople.push( person );
done( null );
})
})
})
async.series( functions, function( err, res ){
test.ifError( err );
test.done();
});
},
databaseDisconnect: function( test ){
db.close();
test.done();
},
}
答案 0 :(得分:0)
回答自己。 解决方案很简单:对于每个驱动程序,这是test.js文件:
var driver = require('./ specificDriver'); var simpledblayerTests = require(“./lib/simpledblayer/test.js”);
var tests = simpledblayerTests.get(
function getDbInfo( done ) {
mw.connect('mongodb://localhost/tests', {}, function( err, db ){
if( err ){
throw new Error("MongoDB connect: could not connect to database");
} else {
done( null, db, driver );
}
});
},
function closeDb( db, done ) {
db.close( done );
}
);
for(var test in tests) {
exports[ test ] = tests[ test ];
}
基本上,实际的“main”模块导出的get()
函数(稍后会详细介绍)有两个参数:打开和关闭数据库连接的两个函数。
get()
函数返回一堆准备导出的函数 - 构成单元测试的函数。
主要模块将具有以下内容:
exports.get = function( getDbInfo, closeDb ){
var tests;
var g = {};
var startup = function( test ){
var self = this;
test.doesNotThrow( function(){
getDbInfo( function( err, db, driver ){
if( err ){
throw( new Error("Could not connect to db, aborting all tests") );
process.exit();
}
// Set the important g.driver variables (db and driver)
g.db = db;
d.driver = driver
test.done();
});
});
}
var finish = function( test ){
var self = this;
closeDb( g.db, function( err ){
if( err ){
throw( new Error("There was a problem disconnecting to the DB") );
}
test.done();
});
};
tests = {
startup: startup,
// ...
// Your tests here. All functions will have access to g.db and g.driver
// ...
finish: finish
}
return tests;
}
这样,每个模块都将运行相同的测试。但是,driver
变量和连接/断开连接到数据库的功能将很简单。
使用实际代码查看它: