I would like to know if my approach is correct, and what is the best practice for similar situation.
Scenario
I am trying to use Sinon.js to stub a function call in my authController, which takes two arguments and process the result in a Callback function to pass it to Next() callback. something similar to this:
// requestController.js
module.exports = function(req, res, next) {
authController.handle(req, res, function(apiMethod) {
if (apiMethod !== undefined || apiMethod !== null) {
apiMethod(next);
} else {
next();
}
});
};
the above method is being called inside my requestController which handles all the requests and needed authController to check the authentication.
The question is how can I use sinon to stub or mock the behavior of authController
and return a fake apiMethod
but the rest of code continues and calls the next()
.
Thanks in advance for your recommendations.
There is still an error. For unit tests your code should look like this:
module.exports = function(req, res, next) {
apiMethod = function() {
//your code goes here
}
authController.handle(req, res, apiMethod);
};
The function wrapped around apiMethod
is redundant
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With