在Jasmine中将参数与函数匹配

4

我想对一个被监听函数的参数执行自定义断言。我能提供一个回调函数,在期望中使用来针对参数吗?

expect(my.method).toHaveBeenCalledWith(jasmine.argumentMatching(myCustomCallback), jasmine.any({}));

...其中jasmine.argumentMatching(myCustomCallback)是伪代码。

1个回答

10
Jasmine的spy有一个属性.calls,提供特定调用的调用次数和参数等信息。请查看文档中的此部分 - 其他跟踪属性。使用哪个取决于你的要求,在文档中已经很好地描述了,但总的来说:
  • .calls.argsFor(n) - returns array of arguments for n-th call

  • .calls.allArgs() - returns array of arrays of arguments for all calls

  • .calls.mostRecent(), .calls.first() - return an object in form:

     { object: my, args: ['foo', 42], returnValue: undefined }
    
  • .calls.all() - returns array of objects (like one above)

一般情况下,它会看起来像这样:
spyOn(my, 'method');

my.method('foo', 42);

expect(my.method.calls.argsFor(0)).toEqual(['foo', 42]);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接