Do you use a ‘Precision Mocker’ like NSubstitute instead of a ‘Monster Mocker’ like Microsoft Fakes?

Last updated by William Liebenberg 11 months ago.See history

Using a precision mocking framework (such as NSubstitute) encourages developers to write maintainable, loosely coupled code.

Mocking frameworks allow you to replace a section of the code you are about to test, with an alternative piece of code. For example, this allows you to test a method that performs a calculation and saves to the database, without actually requiring a database.

There are two types of mocking framework.

The Monster Mocker (e.g. Microsoft Fakes or TypeMock)

This type of mocking framework is very powerful and allows replacing code that wasn’t designed to be replaced. This is great for testing legacy code, tightly coupled code with lots of static dependencies (like DateTime.Now) and SharePoint.

monster mocker
Figure: Bad Example – Our class is tightly coupled to our authentication provider, and as we add each test we are adding *more* dependencies on this provider. This makes our codebase less and less maintainable. If we ever want to change our authentication provider “OAuthWebSecurity”, it will need to be changed in the controller, and every test that calls it

The Precision Mocker (e.g. NSubstitute)

This mocking framework takes advantage of well written, loosely coupled code.

The mocking framework creates substitute items to inject into the code under test.

nsubstitute 1
Figure: Good Example - An interface describes the methods available on the provider

nsubstitute 2
Figure: Good Example - The Product Repository is injected into the ProductService class (via constructor injection)

nsubstitute 3
Figure: Good Example - The code is loosely coupled. The ProductService is dependent on an interface of the Product Repository, which is injected into the ProductService via its constructor. The unit test can easily create a mock object of the Product Repository and substitute it for the dependency. NSubstitute is one of the most popular mocking libraries.

Adam Stephensen
William Liebenberg
We open source. Powered by GitHub