Skip to content

stub.callsArgOnWith(index, object)

Causes the stub to call the argument at the provided index as a callback function, with the argument(s) provided and an additional object parameter to pass the this context.

js
import * as sinon from "sinon";
const person = {
  name: "Mickey Mouse"
};
const stub = sinon
  .stub()
  .callsArgOnWith(0, person, "apple", "banana", "cherry");

function hello(first, second, third) {
  console.log(this.name);
  console.log(first, second, third);
}

stub(hello);
// => Mickey Mouse
// => apple banana cherry

Errors

When the argument at the provided index is not available or is not a function, an Error` will be thrown.

js
import * as sinon from "sinon";
const person = {
  name: "Mickey Mouse"
};
const stub = sinon
  .stub()
  .callsArgOnWith(0, person, "apple", "banana", "cherry");

function hello(first, second, third) {
  console.log(this.name);
  console.log(first, second, third);
}

stub(undefined);
// => Uncaught TypeError: argument at index 0 is not a function: undefined

See also