ts-mockito icon indicating copy to clipboard operation
ts-mockito copied to clipboard

Shortcut: calledOnceAndWithArgs

Open awvalenti opened this issue 4 years ago • 3 comments

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!

awvalenti avatar May 22 '20 11:05 awvalenti

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)

awvalenti avatar May 22 '20 13:05 awvalenti

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?

NagRock avatar Jun 04 '20 11:06 NagRock

Yes, that is correct.

awvalenti avatar Jun 09 '20 23:06 awvalenti