2
我是單元測試的新手。我在Node.js中工作,我正在使用async module。這裏是我正在使用的代碼:使用Mocha/Chai來測試NodeJS的匿名回調代碼
module.exports = {
postYelp: function (cb, results) {
if (results.findLocation) {
results.client.post(['resources', 'Yelp'].join('/'), results.findLocation, function (err, data, ctx) {
/* istanbul ignore next */
if (err) {
cb(err);
} else {
console.log('data', data);
console.log('ctx', ctx.statusCode);
return cb(null, ctx.statusCode);
}
});
}
/* istanbul ignore next */
else cb(null);
},
}
因此,大家可以看到,在函數調用的第三個參數來results.client.post是一個匿名的回調。
我想測試這個回調的覆蓋範圍。如果我可以輕鬆地使用與回調相同的代碼重新創建一個命名的函數並替換它,我可以單獨測試它。但是,封閉函數(「postYelp」)有自己的回調函數(「cb」),它必須在匿名回調函數中使用。
我該如何測試這個匿名函數代碼?