我有这个文件包含以下代码,这是我的api的数据库层。它从外部依赖于SQL Server来获取数据。
var sql = require('mssql');
var async = require('async');
module.exports = {
getDetails: function(number, callback) {
async.parallel({
general: function(callback) {
getGeneral(number, callback);
},
preferences: function(callback) {
getPref(number, callback);
}
},
function(err, results) {
if (err) {
logger.error(err);
throw err;
}
callback(results);
});
}
};
function getGeneral(number, callback) {
var mainconnection = new sql.Connection(dbCredentials[1].generalDBConfig, function(err) {
var request = new sql.Request(mainconnection);
request.input('number', sql.BigInt, number);
request.execute('[number].[genral_get]', function(err, recordsets) {
if (err) {
logger.error(err);
}
var gen = {};
var spResult = recordsets[0];
if (spResult[0] != null) {
spResult.forEach(function(record) {
var test = {};
gen[record.genCode] = record.genValue;
});
callback(null, gen);
} else {
callback(null, null);
}
});
});
}
function getPref(number, callback) {
var mainconnection = new sql.Connection(dbCredentials[0].prefDBConfig, function(err) {
var request = new sql.Request(mainconnection);
request.input('number', sql.BigInt, number);
request.execute('[number].[pref_get]', function(err, recordsets) {
if (err) {
logger.error(err);
}
var spResult = recordsets[0];
if (spResult[0] != null) {
callback(null, spResult[0]);
} else {
callback(null, null);
}
});
});
}
数据库层返回此JSON格式数据:
{
"general": {
"number": "72720604"
"title": "Mr ",
"buildingNameNumber": null,
"subBuildingName": null,
"streetName": null,
"postalTown": null,
"county": null
},
"pref": {
"UseAccessibilitySite": "00000",
"IntroductorySource": "75"
}
};
由于我是单元测试的新手,我不知道如何开始为这个模块编写单元测试,即使选择mocha和chai作为我的单元测试框架。任何建议或帮助表示赞赏...