3

Spies in Sinon - Mastering JS

 2 years ago
source link: https://masteringjs.io/tutorials/sinon/spy
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Spies in Sinon

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');

// Returns 1, which means the real `myFunction()` was called,
// rather than a stub.
assert.equal(spy.getCall(0).returnValue, 1);

More Sinon Tutorials


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK