我正在使用pg-promise.我是学习者,请原谅,如果这对你来说似乎微不足道.我怎样才能进行单元测试.错误输出数据未定义.我一直在js文件中建立连接并导出该module.another js文件用于查询数据库和获取结果集.代码正在按预期工作,我怎么能用mocha和chai编写单元测试.
test1.js
var dbConn= pgp(connUrl);
module.exports = {
getconnect: function () {
return dbConn;
}
};
Run Code Online (Sandbox Code Playgroud)
test2.js
module.exports = {
getData: function (req, res) {
db.getconnect().query(sqlStr, true)
.then(function (data) {
console.log("DATA:", data);
return data;
} } }
Run Code Online (Sandbox Code Playgroud)
unittest.js
describe("Test Cases", function (done) {
it('retrieve response', function (done) {
var req = {};
var res = {};
test2.getData(req, res);
// how would i retrieve value of data from test2.js so i can test
done();
});
});
Run Code Online (Sandbox Code Playgroud)
我如何从unittest.js中的test2.js中检索"数据"值
我一直在使用业力+ requestjs + mocha + chai和sinon.我一直在使用chai-http模块但收到chai.request不是一个函数.请指出我在哪里弄错了我google了很多没有运气呢.
(function() {
var specFiles = null;
var baseUrl = '';
var requirejsCallback = null;
if (typeof window != 'undefined' && window.__karma__ != undefined) {
baseUrl = '/base';
requirejsCallback = window.__karma__.start;
specFiles = [];
for (var file in window.__karma__.files) {
if (window.__karma__.files.hasOwnProperty(file)) {
if (/.*\/js\/spec\/.+_spec\.js$/.test(file)) {
specFiles.push(file);
}
}
}
}
requirejs.config({
baseUrl: baseUrl,
paths: {
'chai': './node_modules/chai/chai',
'sinon': './node_modules/sinon/pkg/sinon',
'chaihttp': './node_modules/chai-http/dist/chai-http',
},
deps: specFiles,
callback: requirejsCallback
});
})();
**Spect-Test.js**
define(['chai', 'sinon', 'chaihttp'], function (chai, …Run Code Online (Sandbox Code Playgroud) javascript ×2
chai ×1
karma-runner ×1
mocha.js ×1
node.js ×1
pg-promise ×1
requestjs ×1
unit-testing ×1