Question

Does NSubstitute have an equivalent to MOQ's VerifyAll call? I'd like to verify that all calls I expect to be received across all substitutes are actually called, ideally in a single TearDown method. I'm currently verifying each received call individually in tests, which isn't ideal. For starters, any calls that are set up on the substitute but that then don't actually get called would slip through the net if they weren't explicitly verified individually.

Was it helpful?

Solution 2

NSubstitute is meant for AAA style tests, not for Record/Replay. As such, it doesn't support them.

OTHER TIPS

What you're describing is the behavior of a Strict mock. Strict mocks, by definition, only allow things you explicitly configure and expect. This creates very brittle tests that tend to break very often, as your code changes, therefore the use if strict mocks are discouraged, and not supported at all by newer frameworks, such as NSubstitute or FakeItEasy.

I'd suggest simply creating two tests for each one of the methods you need to verify: a test that verifies that a certain method was called, then another, that under the same scenario verifies that other method was not called. Thus, in case your logic changes, and one of the methods is called/not called when it should, you'll only get one test broken.

I know this is pretty old and I'm not sure which side I fall in on Loose vs Strict, but for NSubstitute, you can achieve this the following way (xUnit style):

Assert.Empty(_logger.ReceivedCalls());

It shows you all the received calls a particular mock has, so you can just ensure that this number is 0. This may be a newer feature than before, but wanted to make sure it was out there! :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top