ts-mockito
ts-mockito copied to clipboard
Shortcut: calledOnceAndWithArgs
Hello. I would like to verify that my function was called exactly once AND with specific args. I noticed this verification:
verify(MockDocument.addEventListener('keydown', myOnKeyDown)).once()
accepts the following production code
this._document.addEventListener('keydown', myOnKeyDown)
this._document.addEventListener('keydown', null)
once
is being applied only to calls with 'keydown'
and myOnKeyDown
. If other args are used, they are considered another call, and once
doesn't catch it.
As a workaround, I did the following:
verify(MockDocument.addEventListener(anything(), anything())).once()
verify(MockDocument.addEventListener('keydown', myOnKeyDown)).called()
Is there an easier way?
Thanks!
Ideally, I'd like to also make sure that no other calls were made, even with different number of arguments, something like:
verify(MockDocument.addEventListener).singlyCalledWith('keydown', myOnKeyDown))
which would prevent:
document.addEventListener(null, null, null)
Hey, so you would like to be sure that there were no other calls than those that you've verified? Do I understand it correctly?
For example:
// when
foo.sumTwoNumbers(2, 3);
foo.sumTwoNumbers(2, 5);
// then
verify(mockedFoo.sumTwoNumbers(2, 3)).once().AND_NOTHING_ELSE();
Where AND_NOTHING_ELSE
will additionally check that there were no other calls?
Yes, that is correct.