Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSubstitute VerifyAll equivalent

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.

like image 788
levelnis Avatar asked Feb 07 '13 11:02

levelnis


3 Answers

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.

like image 159
Igal Tabachnik Avatar answered Nov 22 '22 11:11

Igal Tabachnik


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

like image 44
Daniel Hilgarth Avatar answered Nov 22 '22 10:11

Daniel Hilgarth


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! :)

like image 31
Daniel Lorenz Avatar answered Nov 22 '22 09:11

Daniel Lorenz