DEV Community

Cover image for Moq vs NSubstitute - Who is the winner?

Moq vs NSubstitute - Who is the winner?

Andres Lozada Mosto on October 16, 2020

In this article will compare two of most important mocking libraries for .Net (.Net Core and .Net Framework) NSubstitute and Moq. What will we com...
Collapse
 
ianwebsteraci profile image
Ian Webster • Edited

Anyone visiting this post should carefully consider using Moq in light of this github.com/moq/moq/pull/1373 it seems the project maintainer feels that illicitly harvesting his users email addresses is a good money making idea.

Collapse
 
andreslozadamosto profile image
Andres Lozada Mosto

Yep
The version 4.20.69 published 2 days ago he removed the reference to sponsor link package but the trust with the community ... Well you know...

Collapse
 
dkarzhounikgdt profile image
Dzmitry Karzhounik

nice post

Collapse
 
countingquoll profile image
countingquoll

I think you forgot another area of syntax where NSubstitute is the clear winner: when referencing the mocked object, e.g. when it is being injected.

var mockedRepo = new Mock<IRepository>();
var sut = new MyClass(mockedRepo.Object);

vs

var mockedRepo = Substitute.For<IRepository>();
var sut = new MyClass(mockedRepo);

Collapse
 
andreslozadamosto profile image
Andres Lozada Mosto

You are right!!
Next week I will update the post and mention you. Thanks!

Collapse
 
iamdorra profile image
Dorra BARTAGUIZ

Hello thanks for this post blog.
But I don't agree with some points.

To throw exceptions, I think that Nsubstitute is the winner because the syntaxe is
moq.Method().Throws();
instead of your suggestion

the example of Multi-returns is not the same, so we feel that the syntaxe of moq is better or equal to Nsubstitute one. But for the example :
mock.SetupSequence(x => x.Users)
.Returns(users1)
.Returns(users2)
.Returns(users3);
the best syntaxe of Nsubstitute would be
mock.Users.Returns(users1, users2, users3);
So for me Nsubstitute is better about multi-returns

Collapse
 
andreslozadamosto profile image
Andres Lozada Mosto

Hi Dorra, do you have good points here

Let me check about the Throws...

Regarding Multi-Returns, in my opinion Moq has a more readable syntax, but is only my opinion and I'm very glad to hear that you have your own point of view 🙂

Collapse
 
adam_b profile image
Adam Braimah

This is suddenly even more pertinent with the controversy over the latest versions of Moq having SponsorLink attached - I think a lot of people will be looking at alternatives like NSubstitute and FakeItEasy!

Collapse
 
victoryarema profile image
Victor Yarema • Edited

The code one needs to write for callbacks testing in NSubstitute is just an unreadable and unmaintainable garbage. Especially when it comes to arguments.

Collapse
 
jasonbock profile image
Jason Bock

FWIW I've written a mocking framework that's based on the source generator feature in C#. It's called Rocks - feel free to check it out and let me know what you think.

github.com/JasonBock/Rocks/
nuget.org/packages/Rocks

Collapse
 
itsmash profile image
Mohammod Al Amin Ashik

If you add up mocking virtual protected method/property, logger mocking(with ILogger.Moq), and casting comparision, Moq will definitely be the winner.