stub.callsArg(index) from Sinon.JS 是做什么的

What does stub.callsArg(index) from Sinon.JS do?

本文关键字:JS 什么 Sinon from callsArg index stub      更新时间:2023-09-26

说真的,我无法弄清楚这一点。文档为我们提供了:

stub.callsArg(index) - 使存根在提供的索引处调用参数作为回调函数。 stub.callsArg(0); 导致存根调用第一个参数作为回调。

但是,我不知道要索引的参数列表在哪里。也许我只是不明白存根是什么...

根是具有可编程行为的noop函数。在您的情况下callsArg(index)将对存根进行编程,使其期望在index处获得函数并立即调用它。

function sayHi() {
  console.log('hi');
}
var stub = sinon.stub().callsArg(2);
stub('abc', 42, sayHi); // prints "hi"