0
我正在尝试为我的node.js代码编写一个单元测试。我能够编写一个函数的测试,但我想测试每个网络查询(查询从数据库中获取数据)是否返回所需的响应。 我正在分享我的node.js代码以及我的测试代码。使用mocha,node.js强化测试函数
Node.js的代码(routes.js)
module.exports = {
getUser: {
get: function(req, parameters, environment) {
var id = req.queryString.id;
return new Promise(function(resolve, reject) {
db.tx(t =>{
return t.one("select configuration_value from fint.configuration where configuration_key='LOAN' and application_id =(select application_id from fint.application where application_name='Product Tailoring')")
})
.then(conf => {
conf_key = conf.configuration_value;
})
db.tx(t =>{
return t.one("select smart_account_type_id from fint.smart_account_type where smart_account_type_name='LOAN'")
})
.then(acc_type => {
smart_acc_type_id = acc_type.smart_account_type_id;
})
}
})
}
}
这是一个示例代码,正是我想要的是运行在单独的查询,而不是整体功能测试。
我的测试代码(test.js)
var expect = require('chai').expect;
var Promise = require('bluebird');
var chai = require('chai');
var app = require('../api/smartAccount/identifyLoan/getDirectDebitTrans/routes');
describe("Unit testing for function", function(){
it("Testing the function using mocha", function(done){
var req = {
queryString: {
uniqueUserId: 101
}
};
var test = app.getUser.get(req);
return expect(Promise.resolve(test)).to.have.property('_bitField');
done();
});
});
任何线索可以理解的,如果有人越过关于同一些链接过来,请分享。 TIA
谢谢,它有帮助。 诺言解决了也拒绝,但我没有把这部分放在代码中,因为我主要关心的是在单个函数中测试多个网络查询。 只是出于好奇,是否有可能在单个函数中测试多个查询? –
你可以测试任何你想要的。给一个输入,测试输出。按照定义,单元测试是测试一个单元,这样做会使测试简单明了。为什么要在单个函数中测试多个查询? – Cyril
其实它更多的是我想测试函数内部的函数,即我想写我的测试用例,因为它测试了写入测试用例的根函数内部的多个函数。你能提供一点知识吗? –