Sinon Spy On Function Not Working
I'm trying to write a standalone test for this simple middleware function function onlyInternal (req, res, next) { if (!ReqHelpers.isInternal(req)) { return res.status(HttpSt
Solution 1:
Sinon cannot change content of existing function, so all spies it creates are just wrappers over existing function that count calls, memoize args etc.
So, your first example is equal to this:
function next() {}
let spy = sinon.spy(next);
next(); // assuming that middleware is just calling next
// spy is not used!
Your second example, equals to this:
let next = { next: () => {} }
next.next = sinon.spy(next.next); // sinon.spy(obj, 'name') just replaces obj.name with spy on it
next.next(); // you actually call spy which in in turn calls original next.next
//spy is called. YaY
So, they key to 'spying' and 'stubbing' in sinon is that you have to use spied/stubbed function in test. Your original code just used original, non-spied function.
Post a Comment for "Sinon Spy On Function Not Working"