docs/concepts/stubs/api/calls-arg-on-with-async.md
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.
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);
When the argument at the provided index is not available or is not a function, an Error will be thrown.
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