How to Migrate from Moq to NSubstitute

I was a happy user of the Moq library for the last 12 years. I liked the simplicity of Moq and how it allowed me to write tests first and then incrementally build the functionality.

Unfortunately, the creator of Moq is currently doing his best to kill the project. As he declared, either his spyware SponsorLink strong-arms enough individual developers into sponsoring him, or he will quit developing Moq. This leaves us in a lose-lose situation and for me it is time to move my private projects to another mocking library.

I played with a few alternatives and finally settled with NSubstitute. It covers all my use cases and offers an even simpler syntax than what I know from Moq.

 

Installing NSubstitute

We can install NSubstitute into our test projects using the NuGet dialog from Visual Studio or with this command in the Package Manager Console:

I strongly suggest you read through the documentation at least once if you are new to NSubstitute. There you find everything you need to start with NSubstitute and the well-crafted examples are to the point and without any distractions.

 

Preparations

We use an interface (that we are going to mock), a class to store data and a service that we use as a stand in for our business classes:

The method names of the service class are by design not an exact match to the ones in the interface. I did this to make it explicit that what we mock is a mock-implementation of the interface and not a change of the service class.

 

Mock the return value of a method

We can create a mock for our interface and define what values should be returned if this method gets called:

When we call the mocked method .All() through our service, we get the two instances back.

If our method requires parameters, but we do not care what those parameters are, we can use Arg.Any<int>() as a stand in. It does now not matter what Id we pass to the method and always get the same data back:

 

Verify calls to a method

If we want to know if a method was called by our system under test, we can ask our mock with the Received() method for our expected number of method calls:

If the method gets called more often or less often than we specify, then our test will fail with this message:

NSubstitute.Exceptions.ReceivedCallsException : Expected to receive exactly 2 calls matching:
GetById(any Int32)
Actually received 1 matching call:
GetById(4)

The method DidNotReceive() helps us to make sure that a certain method was not called.

 

Throw exceptions on method calls

Sometimes we want to simulate an error state and throw an exception. We can do this in NSubstitute with the Throws() method:

 

Change return values between calls

If we need different return values between method calls, we can write those values as a comma separated list of parameters in the return() method:

 

Get the argument of a method call

If we need to know with which parameters our method was called, we can use this code to put them into a list:

We can use the same technique to replace values for the returned mocks on the fly:

This little trick allows us to return an instance that has always a matching Id to the one that was requested in the parameter.

 

Alternatives

If you want to move away from Moq but prefer something else than NSubstitute, you should explore FakeItEasy. It offers more flexibility but needs a bit more knowledge to work effectively.

 

Conclusion

NSubstitute supports all my use-cases that I have when it comes to mocking. For many parts, I favour the syntax of NSubstitute over Moq. Should you want to switch to a mocking framework that focuses on mocking, I highly recommend you switch to NSubstitute.

3 thoughts on “How to Migrate from Moq to NSubstitute”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.