我需要为瀑布中使用的函数编写单元测试,因此它们必须在瀑布外部定义身体,但是下面(试验)代码无法运行,我有什么遗漏吗?
async.waterfall([
one(cb),
two(cb)
],function(){});
var function one(cb){cb(null)}
var function one(cb){cb(null)}
答案 0 :(得分:0)
只需将函数编写为变量并引用它们:
var one = function (cb) {console.log('1'); cb(null); }
var two = function (cb) {console.log('2'); cb(null); }
async.waterfall([
one,
two
], function() {
console.log('done');
});
答案 1 :(得分:0)
编辑:
async.waterfall([
one,
two,
], function (err, result) {
// result now equals null
});
function one(cb) {
cb(null, null);
}
function two(arg1, cb) {
cb(null, arg1);
}
尝试使用demo:
async.waterfall([
function(callback) {
callback(null, 'one', 'two');
},
function(arg1, arg2, callback) {
// arg1 now equals 'one' and arg2 now equals 'two'
callback(null, 'three');
},
function(arg1, callback) {
// arg1 now equals 'three'
callback(null, 'done');
}
], function (err, result) {
// result now equals 'done'
});
//或者,使用命名函数:
async.waterfall([
myFirstFunction,
mySecondFunction,
myLastFunction,
], function (err, result) {
// result now equals 'done'
});
function myFirstFunction(callback) {
callback(null, 'one', 'two');
}
function mySecondFunction(arg1, arg2, callback) {
// arg1 now equals 'one' and arg2 now equals 'two'
callback(null, 'three');
}
function myLastFunction(arg1, callback) {
// arg1 now equals 'three'
callback(null, 'done');
}
详细了解Link