blog tag

Moq vs NSubstitute

Description: Moq vs NSubstitute favoring NSubstitute
Publish Date: 11/19/2017
Keywords: Moq NSubstitute Testing Test
There are two popular mocking frameworks called Moq and NSubstitute, among many. Both work great  and the negative consequences of choosing one over the other are small. But there are differences and the purpose of this post is to contribute to that general discussion.

Moq Documentation
NSubsitutte Documentation

The interwebz have already done some great comparisons.
https://weareadaptive.com/2014/09/30/why-nsubstitute/
http://blog.netronica.io/nsubstitute-vs-moq-vs-fakeiteasy/
https://www.google.com/search?client=opera&q=moq+vs+nsubstitute&sourceid=opera&ie=UTF-8&oe=UTF-8

The two main points of this post is that the extra setup syntax of Moq is subjectively negative and that NSubstitute supports named parameters and Moq doesn't.


Named Parameters:
Named parameters are a feature of C# and documented here . In general named parameters can make your code more readable when the name of the input parameter contributes to the readability of the calling code. Stackoverflow has a couple examples.
NSubstitite supports them but Moq does not.

Setup Syntax:
As this and other examples show, you will spend time writing lambda syntax and fetching the usable object (mock.Object) when using Moq as opposed to NSubstitute.

Conclusion: It's not a show stopper to choose either, but if you like named params and don't like the extra setup syntax then NSubstitute might be your winner.
Github Source
Comments: 0
© Chadwick 2021