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

Jasmine : spy on a function called X times, and get the n-th call

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

这是一个代码示例,您可以解决问题:

// 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);

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

expect(object.myFunction).nthCall(2).toHaveBeenCalledWith('', 0, true);

???

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