我知道与sinon.js https://sinonjs.org您可以测试间谍是否被呼叫一定次数:
sinon.assert.calledTwice(mySpy.someMethod);
您可以测试是否使用某些参数调用了间谍:
sinon.assert.calledWith(mySpy.someMethod, 1, 2);
但是如何将它们组合起来测试某个方法是否被使用特定参数调用了特定次数?从理论上讲,是这样的:
sinon.assert.calledTwiceWith(mySpy.someMethod, 1, 2);
A spy
提供对使用它进行的调用的访问getCall() and getCalls() https://sinonjs.org/releases/v6.1.5/spies/#var-spycall--spygetcalln. Each Spy call https://sinonjs.org/releases/v6.1.5/spy-call/可以使用类似方法进行测试calledWithExactly() https://sinonjs.org/releases/v6.1.5/spy-call/#spycallcalledwithexactlyarg1-arg2-:
import * as sinon from 'sinon';
test('spy', () => {
const spy = sinon.spy();
spy(1, 2);
spy(3, 4);
expect(spy.callCount).toBe(2);
expect(spy.getCall(0).calledWithExactly(1, 2)).toBe(true);
expect(spy.getCall(1).calledWithExactly(3, 4)).toBe(true);
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)