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...
For further actions, you may consider blocking this person and/or reporting abuse
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.
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...
nice post
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);
You are right!!
Next week I will update the post and mention you. Thanks!
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
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 🙂
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!
The code one needs to write for callbacks testing in NSubstitute is just an unreadable and unmaintainable garbage. Especially when it comes to arguments.
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
If you add up mocking virtual protected method/property, logger mocking(with ILogger.Moq), and casting comparision, Moq will definitely be the winner.