I know with sinon.js you can test that a spy was called a certain number of times:
sinon.assert.calledTwice(mySpy.someMethod);
And you can test that a spy was called with certain arguments:
sinon.assert.calledWith(mySpy.someMethod, 1, 2);
But how to you combine them to test that a method was called a specific number of times with specific arguments? Something, theoretically, like this:
sinon.assert.calledTwiceWith(mySpy.someMethod, 1, 2);
A spy
provides access to the calls made to it using getCall()
and getCalls()
. Each Spy call can be tested using methods like calledWithExactly()
:
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);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With