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
其實它更多的是我想測試函數內部的函數,即我想寫我的測試用例,因爲它測試了寫入測試用例的根函數內部的多個函數。你能提供一點知識嗎? –