May 18, 2022

Blog @ Munaf Sheikh

Latest news from tech-feeds around the world.

Spies in Sinon – Mastering JS


Feb 1, 2022

Sinon spies are used to record information about function calls.
Unlike mocks or stubs, spies do not replace the function being called.
Spies just record what parameters the function was called with, what value it returned, and other information about the function execution.

const sinon = require('sinon');
const assert = require('assert');

let calls = 0;
let obj = {
  myFunction: function(data) {
    return ++calls;
  }
};

const spy = sinon.spy(obj, 'myFunction');

obj.myFunction('test');

assert.equal(spy.getCall(0).args[0], 'test');



assert.equal(spy.getCall(0).returnValue, 1);

More Sinon Tutorials



Source link