问题出在这里:我有WPSManager类,它被导出为模块:
function WPSManager(dbclient) {
var databaseclient = dbclient;
this.getWrappers = function(excludeid, callback) {
var query = "SELECT * FROM public.wps_methods WHERE id <> " + excludeid + " AND id > 88;";
databaseclient.query(query, function(error, rows, fields) {
callback(rows.rows);
});
} //end getWrappers()
} //end class
module.exports = WPSManager;
如何测试查询结果?我尝试使用 nodeunit (在getWrappers()
的回调中提供断言),以及 mocha 的变体(下面的示例)。所有这些都没有在我提供的回调中做出任何断言。
delete require.cache;
var assert = require("assert");
var pg = require("pg");
var WPSManager = require("./WPSManager");
/**
* Database client initiation.
*/
var dbclient = new pg.Client("***");
dbclient.connect();
describe('WPSManager', function(){
describe('getWrappers()', function(){
it('should return list of wrappers', function(){
var wpsmanager = new WPSManager(dbclient);
wpsmanager.getWrappers(14, function(data) {
// This assertion is not performed
assert.equal(5, data.length);
done();
});
})
})
});
所以,这里有一个问题 - NodeJS的任何单元测试工具都可以使用 assert()来解决这个问题,或者只有可用的东西吗?断言很棒,但它并不那么花哨:)
答案 0 :(得分:1)
使用jasmine-node(2.0.0)我会像下面这样。 我嘲笑你的数据库,但它也应该与真实的东西一起工作。
function WPSManager(dbclient) {
var databaseclient = dbclient;
this.getWrappers = function(excludeid, callback) {
var query = "SELECT * FROM public.wps_methods WHERE id <> " + excludeid + " AND id > 88;";
databaseclient.query(query, function(error, rows, fields) {
callback(rows.rows);
});
} //end getWrappers()
} //end class
var dbclient = {
query : function ( query, callback ) {
setTimeout( function () {
callback( null, { rows:Array(5) }, null );
}, 500);
}
};
//var dbclient = mockDB;
//var dbclient = new pg.Client("***");
//dbclient.connect();
describe('WPSManager', function(){
describe('getWrappers()', function(){
it('should return list of wrappers', function (done) {
var wpsmanager = new WPSManager(dbclient);
wpsmanager.getWrappers(14, function(data) {
expect(data.length).toBe(5);
done();
});
});
});
});