Moq Minute - Verify over Setup

Reading time ~1 minute

I love Moq. I think it is the best .NET mocking library. I prefer using Verify to ensure the correct behaviour instead of using a strict Setup.

When I started mocking, the setup for each mock often mirrored the exact method calls I expected. Another bad habit was heavily using Verifiable mocks to validate the right methods were called. Both practices made my tests and code more brittle.

1
2
3
4
5
6
Mock<IExample> mock = new Mock<IExample>( MockBehavior.Strict );
mock.Setup( m => m.Run( 7 ) );

IExample target = mock.Object;

target.Run( 7 );

Instead, I have switched to using looser mocks and verifying only important methods:

1
2
3
4
5
6
7
Mock<IExample> mock = new Mock<IExample>();

IExample target = mock.Object;

target.Run( 7 );

mock.Verify( m => m.Run( 7 ) );

The big difference is line 7 from the second sample, compared to line 2 from the first sample. This subtle change shifts when the mocked behaviour is validated to the Verify statement instead of when the mocked method is called by the code you are testing.

The looser mocks make the code less fragile. By being more accepting of different cases the mocks can allow more potential implementations. In most cases the extra strictness is not required and gets in the way.

The test clearly separates the setup from the assertions as recommend by AAA. You can see exactly what is expected in the assertions instead of doing everything in the setup.

When a test fails the verification pinpoints exactly why. My previous style, using Setup, would instead throw a weird exception when the method is called or list of one many verifiable methods which were not called as expected.


Now when I used mocking in tests the mocks are much looser and I only verify the calls I care about. Try it out and see if you like this style too!


I would like to thank my former coworker, Jeff, who first showed me this approach and why it was better.

The Worst Week of My Life

In January I had the worst week of my life. My wife and I joked we wanted to start 2016 in February. Within a single week I lost my job a...… Continue reading