最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What does stub.callsArg(index) from Sinon.JS do? - Stack Overflow

programmeradmin3浏览0评论

Seriously, I can't figure this out. The documentation gives us:

stub.callsArg(index) - Causes the stub to call the argument at the provided index as a callback function. stub.callsArg(0); causes the stub to call the first argument as a callback.

However, I've got no idea where this list of arguments to be indexed into is. Maybe I just don't understand what a stub is...

Seriously, I can't figure this out. The documentation gives us:

stub.callsArg(index) - Causes the stub to call the argument at the provided index as a callback function. stub.callsArg(0); causes the stub to call the first argument as a callback.

However, I've got no idea where this list of arguments to be indexed into is. Maybe I just don't understand what a stub is...

Share Improve this question asked Mar 16, 2015 at 19:15 Zachary ForsterZachary Forster 1191 silver badge4 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

A stub is a noop function with programmable behavior. In your case callsArg(index) will program the stub to expect a function at index and immediately invoke it.

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

评论列表(0)

  1. 暂无评论