存根使用 Sinon 执行回调的方法
Stub a method that does a callback using Sinon
我想知道我的方法是否正确,以及类似情况的最佳做法是什么。
场景
我正在尝试使用 Sinon.js 在我的 authController 中存根函数调用,它接受两个参数并在回调函数中处理结果以将其传递给 Next() 回调。类似这样的东西:
// requestController.js
module.exports = function(req, res, next) {
authController.handle(req, res, function(apiMethod) {
if (apiMethod !== undefined || apiMethod !== null) {
apiMethod(next);
} else {
next();
}
});
};
在我的 requestController 中调用上述方法,它处理所有请求并需要 authController 来检查身份验证。
问题 是如何使用 sinon 来存根或模拟 authController
和 return 的行为,但 apiMethod
其余代码继续并调用 next()
。
提前感谢您的建议。
还是有错误。对于单元测试,您的代码应如下所示:
module.exports = function(req, res, next) {
apiMethod = function() {
//your code goes here
}
authController.handle(req, res, apiMethod);
};
环绕apiMethod
的函数是多余的
我想知道我的方法是否正确,以及类似情况的最佳做法是什么。
场景
我正在尝试使用 Sinon.js 在我的 authController 中存根函数调用,它接受两个参数并在回调函数中处理结果以将其传递给 Next() 回调。类似这样的东西:
// requestController.js
module.exports = function(req, res, next) {
authController.handle(req, res, function(apiMethod) {
if (apiMethod !== undefined || apiMethod !== null) {
apiMethod(next);
} else {
next();
}
});
};
在我的 requestController 中调用上述方法,它处理所有请求并需要 authController 来检查身份验证。
问题 是如何使用 sinon 来存根或模拟 authController
和 return 的行为,但 apiMethod
其余代码继续并调用 next()
。
提前感谢您的建议。
还是有错误。对于单元测试,您的代码应如下所示:
module.exports = function(req, res, next) {
apiMethod = function() {
//your code goes here
}
authController.handle(req, res, apiMethod);
};
环绕apiMethod
的函数是多余的