Are fakes better than Mocks?

C#Unit TestingMockingFakeiteasy

C# Problem Overview


I stumbled upon this open source project Fake It Easy, and I have to admit, it looks very interesting, however I have my doubts, what are the difference between FIE fakes and say Moq Mocks? Is any one better for particular uses?

EDIT:

What is it about this new framework that would make it better than say Moq?

C# Solutions


Solution 1 - C#

To be clear, I created FakeItEasy so I'll definitely not say whether one framework is better than the other, what I can do is point out some differences and motivate why I created FakeItEasy. Functionally there are no major differences between Moq and FakeItEasy.

FakeItEasy has no "Verifiable" or "Expectations" it has assertions however, these are always explicitly stated at the very end of a test, I believe this makes tests easier to read and understand. It also helps beginners to avoid multiple asserts (where they would set expectations on many calls or mock objects).

I used Rhino Mocks before and I quite liked it, especially after the AAA-syntax was introduced I did like the fluent API of Moq better though. What I didn't like with Moq was the "mock object" where you have to use mock.Object everywhere, I like the Rhino-approach with "natural" mocks better. Every instance looks and feels like a normal instance of the faked type. I wanted the best of both worlds and also I wanted to see what I could do with the syntax when I had absolutely free hands. Personally I (obviously) think I created something that is a good mix with the best from both world, but that's quite easy when you're standing on the shoulders of giants.

As has been mentioned here one of the main differences is in the terminology, FakeItEasy was first created to introduce TDD and mocking to beginners and having to worry about the differences between mocks and stubs up front (the way you would have to in Rhino) is not very useful in my opinion.

I've put a lot of focus into the exception messages, it should be very easy to tell what whent wrong in a test just looking at an exception message.

FakeItEasy has some extensibility features that the other frameworks don't have but these aren't very well documented yet.

FakeItEasy is (hopefully) a little stronger in mocking classes that has constructor arguments since it has a mechanism for resolving dummy-values to use. You can even specify your own dummy value definitions by implementing a DummyDefinition(Of T) class within your test project, this will automatically be picked up by FakeItEasy.

The syntax is an obvious difference, which one is better is largely a matter of taste.

I'm sure there are lots of other differences that I forget about now (and to be fair I have never used Moq in production myself so my knowledge of it is limited), I do think these are the most important differences though.

Solution 2 - C#

The terminology used in testing can be slightly confusing. The best source explaining the difference between different concepts is Mocks Aren't Stubs by Martin Fowler. In summary, fake is a generic term that describes both stubs and mocks.

Solution 3 - C#

The terminology in mocking can be confusing - and sometimes is quite unintuitive.

Therefore, many people proposed a simpler, new terminology, where you have only fakes, mocks, and stubs.

Fake is the generic term for all possible kinds of test doubles, no matter where they come from and how they are used.

Beyond that, fakes are distinguished only along one single dimension: whether they influence test outcome or not; or, in other words: whether you have to set up return values for the fake, which are somehow used during test execution, or it is a 'silent' object which only serves to fulfill some dependency.

Stub it is that 'silent' object.

Mock it is actively participates in test execution

Beyond that, there's no further distinction - which surely has its historical merits, but is now largely counter-intuitive and academical, and it's kind of obfuscating really important concepts of Test-driven development.

Concerning the comparison between Moq and FakeItEasy: the two frameworks are largely the same from a conceptual point of view - the differences are only in the API and in the terminology...

Thomas

Solution 4 - C#

From my point of view Fake will not cancels the Moc for example I use Dev Magic Fake to fake DAL and Business layer and in the same time I use Mock in MVC to for HTTPContext

var repoistory = new FakeRepository<ProductTypeForm, VendorForm>();
            repoistory.Save(productTypeForm);
            this.FillDropDown(new FakeRepository<VendorForm>());

In the previous code Dev Magic Fake will save the ProductTypeForm and retrieve the VendorForm from Dev Magic Fake and link it to ProductTypeForm, this save operation can be permanent

For more inforamtion about Dev Magic Fake see it on CodePlex: http://devmagicfake.codeplex.com

Te test this method we Have to Mock the HTTP context

var context = new Mock<HttpContextBase>();
var request = new Mock<HttpRequestBase>();

So I work with fake and mock

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFrancisco NoriegaView Question on Stackoverflow
Solution 1 - C#Patrik HägneView Answer on Stackoverflow
Solution 2 - C#Adam ByrtekView Answer on Stackoverflow
Solution 3 - C#Thomas WellerView Answer on Stackoverflow
Solution 4 - C#Mohamed.Radwan -MVPView Answer on Stackoverflow