I'm trying to test that certain function is called in a callback, however I don't understand how to wrap the outer function. I'm using mocha as my testing suite, with chai as my assertion library, and sinon for my fakes.
fileToTest.js
const externalController = require('./externalController');
const processData = function processData( (req, res) {
externalController.getAllTablesFromDb( (errors, results) => {
// call f1 if there are errors while retrieving from db
if (errors) {
res.f1();
} else {
res.f2(results);
}
)};
};
module.exports.processData = processData;
In the end I need to verify that res.f1 will be called if there are errors from getAllTablesFromDb, and res.f2 will be called if there are no errors.
As can be seen from this snippet externalController.getAllTablesFromDb is a function that takes a callback, which in this case I have created using an arrow function.
Can someone explain how I can force the callback down error or success for getAllTablesFromDb so that I can use a spy or a mock to verify f1 or f2 was called?