NagRock / ts-mockito

Mocking library for TypeScript
MIT License
969 stars 93 forks source link

Shortcut: calledOnceAndWithArgs #185

Open awvalenti opened 4 years ago

awvalenti commented 4 years ago

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 commented 4 years ago

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)
NagRock commented 4 years ago

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?

awvalenti commented 4 years ago

Yes, that is correct.