11

NSubstitute vs Moq vs FakeItEasy vs Rhino

 9 months ago
source link: https://www.stevefenton.co.uk/blog/2014/12/nsubstitute-vs-moq-vs-fakeiteasy-vs-rhino/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

NSubstitute vs Moq vs FakeItEasy vs Rhino

The long and short of this article is that I tried out a whole load of mocking frameworks and decided I liked NSubstitute the best. This is currently a reasonably simple decision, because on the whole, Moq, FakeItEasy, Rhino, JustMock and many others besides are all essentially syntactical best friends.

There may be some minor differences between “A.Fake”, “MockRepository.GenerateStub”, “Mock.Create” and “new Mock” but when you consider that this line of code sums them all up in one hit – you can see they are all in the same club.

mock.Setup(m => m.DoSomething()).Returns("Value");

This is specifically the Moq version, but asides from the two words that precede the lambda – this is what you’ll find almost everywhere. You may remember being warned to “beware yellow snow” when using ASP.NET templating, well in this case you get a sea of “((m => m))” or “(() => )” emoticons.

Here is the full Moq version for use when comparing with NSubstitute later. I wrote versions of this same simple scenario in something like eight mock frameworks.

var mock = new Mock<GameLanguage>();
mock.Setup(m => m.DoSomething()).Returns("Value");

var language = mock.Object;
language.Fizz = "Fizz";
language.Buzz = "Buzz";

_target = new Game(language);

Enter NSubstitute and its neat use of extension methods.

language.DoSomething().Returns("Value");

I prefer this syntax. A lot.

Here is the complete NSubstitute version.

var language = Substitute.For<GameLanguage>();

language.DoSomething().Returns("Value");
language.Fizz = "Fizz";
language.Buzz = "Buzz";

_target = new Game(language);
Steve Fenton
Written by Steve Fenton
Saturday, December 20, 2014
Revised Wednesday, August 9, 2023

Steve Fenton is an Octonaut at Octopus Deploy and six-time Microsoft MVP for developer technologies. He’s a Software Punk and writer.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK