【发布时间】:2018-11-07 01:36:04
【问题描述】:
我正在使用 sinon 在 nodejs 中进行单元测试。
如何测试我的 task1.js 的 else 部分?假设如果我的计数不为 0,我将继续我自己的处理,否则我结束我的任务。
我可以模拟我的 getbookForUpdateCount 以返回计数值,但接下来会发生什么?
我如何确保走这条路?
Task1.js:
function updateInfo() {
bookDao.getbookForUpdateCount(updateCountParams, function (results) {
console.log("Step 1: " + results[0].RECCOUNT + " for updates!".blue);
//1. Get count. If > 0, proceed. else end task.
if (results[0].RECCOUNT > 0) {
...
//some other functions
}
else {
//no record to be processed.
return;
}
});
}
【问题讨论】:
-
您需要进行两项测试,一项用于测试条件是否为真,另一项用于测试条件是否为真。我假设您正在使用
sinon.stub().yields()的某些变体?如果是这样,您只需要更改存根产生的内容。你能分享一下你目前的测试代码吗?
标签: node.js unit-testing sinon