Moq vs NSubstitute

Keywords: Moq, NSubstitute, Testing, Test
Publish Date: 11/19/2017
Description: Moq vs NSubstitute favoring NSubstitute

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.

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

Leave a Comment:

    Captcha Challenge