Jasmine :监视一个名为 X 次的函数,并获得第 n 个调用

5 javascript expectations jasmine

我似乎无法在网上找到解决方案。

这是一个代码示例,因此您会遇到问题:

// Spy on the wanted function
spyOn(object, 'myFunction');

// Call it 3 times with different parameters
object.myFunction('');
object.myFunction('', 0);
object.myFunction('', 0, true);

// Now all of these expects work
expect(object.myFunction).toHaveBeenCalledTimes(3);
expect(object.myFunction).toHaveBeenCalledWith('', 0);
expect(object.myFunction).toHaveBeenCalledWith('');
expect(object.myFunction).toHaveBeenCalledWith('', 0, true);
Run Code Online (Sandbox Code Playgroud)

我想测试是否每次调用都正确。有没有办法说这样的话?

expect(object.myFunction).nthCall(2).toHaveBeenCalledWith('', 0, true);
Run Code Online (Sandbox Code Playgroud)

???

k10*_*102 7

calls属性,你可以像使用: expect(object.myFunction.calls.argsFor(2)).toEqual(['', 0, true])

  • 哦等等:在我的 IDE 中,该函数仍然被键入为一个类。我使用了 `(object.myFunction as jasmine.Spy)`,它给我打电话了!感谢您的帮助 :) (2认同)