Back to Sinon

assert.calledOn

docs/concepts/assertions/api/called-on.md

22.1.01.3 KB
Original Source

assert.calledOn(spyOrSpyCall, object);

Passes, when the fake, spy or stub was called at least once with object as this.

It's possible to assert on a dedicated spy call: sinon.assert.calledOn(call, arg1, arg2, ...);.

js
import * as sinon from "sinon";
const spy = sinon.spy();
const object = {
  name: "Apple Pie"
};

// Has not been called yet, so the error message is a bit short :)
sinon.assert.calledOn(spy, object);
// => Uncaught: Error [AssertError]: expected spy to be called with { name: 'Apple Pie' } as this but was called with

spy.call(object);

// Generates no error
sinon.assert.calledOn(spy, object);

// Generates no error
sinon.assert.calledOn(spy.firstCall, object);

Asserting on a spyCall

js
import * as sinon from "sinon";
const spy = sinon.spy();
const object = {
  name: "Apple Pie"
};

spy.call(object);

// get a spyCall instance
const call = spy.firstCall;

// Generates no error
sinon.assert.calledOn(call, object);

See Function.prototype.call().

Example using test framework

<<< ../../../.vitepress/tests/docs/assertions/api/called-on.test.js