moq icon indicating copy to clipboard operation
moq copied to clipboard

Add Way to Exclude Calls from VerifyNoOtherCalls

Open rarcher opened this issue 1 year ago • 7 comments

When verifying calls on mocks, it often happens that there is some trivial operation (like a property read) that you really don't care about, and the executing code is allowed to invoke any number of times. However, because of the way VerifyNoOtherCalls works, there's no way to mark things as ignored - you have to explicitly specify each individual call, which leads to unit tests either being too tightly coupled to the main code base, or using hackish work-arounds (like doing Times.AtMost(1000)). There ought to be a way to tell Moq that such-and-such a call just isn't relevant.

Back this issue Back this issue

rarcher avatar Jul 17 '24 14:07 rarcher

As side note, the most obvious way to implement this would be to let Times.AtLeast accept a 0 value as a parameter.

rarcher avatar Jul 17 '24 14:07 rarcher

Wouldn't turn your mock into a loose mock and only verify what you're interested in work better in this case?

kzu avatar Aug 22 '24 05:08 kzu

For situations where there aren't many methods you need to verify, sure. But the use case for this is when you have a LOT of functions, and you want to verify that most of them are never touched, with the possible exception of a few accessors. In that scenario, it's much more convenient to handle everything in bulk, using VerifyNoOtherCalls.

rarcher avatar Aug 22 '24 22:08 rarcher

If most should not be called, then a strict mock with no setups for them seems like a better approach?

kzu avatar Aug 26 '24 15:08 kzu

Hmmmm. Fair point. Maybe this is more of a documentation issue then? It is not obvious that strict mocks are the way to handle this case, for someone with limited familiarity with the library.

rarcher avatar Aug 28 '24 20:08 rarcher

Yeah, perhaps. I guess this is something that is typically covered by the multiple blogs/stackoverflow/online curses you can find by now on using Moq itself. But I'll keep that in mind for vNext.

Feel free to close this issue if you feel the strict mock will suffice.

Thanks!

kzu avatar Sep 02 '24 05:09 kzu

Due to lack of recent activity, this issue has been labeled as 'stale'. It will be closed if no further activity occurs within 30 more days. Any new comment will remove the label.

github-actions[bot] avatar May 10 '25 01:05 github-actions[bot]

This issue will now be closed since it has been labeled 'stale' without activity for 30 days.

github-actions[bot] avatar Jun 09 '25 01:06 github-actions[bot]