我想知道我的方法是否正确,以及在类似情况下的最佳做法是什么。
情境
我正在尝试使用Sinon.js在我的authController中存根函数调用,该函数接受两个参数并在Callback函数中处理结果以将其传递给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
并返回伪造的东西,apiMethod
但其余代码继续并调用next()
。
预先感谢您的建议。
仍然有一个错误。对于单元测试,您的代码应如下所示:
module.exports = function(req, res, next) {
apiMethod = function() {
//your code goes here
}
authController.handle(req, res, apiMethod);
};
包裹的功能apiMethod
是多余的
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句