Skip to content

stub.callsArgOnWithAsync(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, asynchronously.

js
import * as sinon from "sinon";
const person = {
  name: "Mickey Mouse"
};
const stub = sinon.stub().callsArgOnWithAsync(0, person, "Donald Duck");

function rename(newName) {
  this.name = newName;
}

stub(rename);
console.log(person.name);
// => "Mickey Mouse"

setTimeout(function () {
  console.log(person.name);
  // => "Donald Duck"
}, 1);

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().callsArgOnWithAsync(0, person, "Donald Duck");

function rename(newName) {
  this.name = newName;
}

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

See also

More information