Are fakes better than Mocks? [closed]
Asked Answered
S

4

54

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?

Stool answered 22/10, 2010 at 21:28 Comment(1)
For a totally different kind of mocking and stubs look at the Microsoft moles framework (research.microsoft.com/en-us/projects/moles). Moles allows mocking static, sealed and third party classes as well.Sadoc
R
113

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.

Rafaellle answered 13/11, 2010 at 20:20 Comment(6)
+1 nothing beats a post by the author :-)Bush
Thanks @Patrick! great answer, this really helps to understand the reasoning behind the project, you should add something like this into the wiki, for people who already use a framework and could be interested in switching :) btw, have you dogfood FIE with your own production code?Stool
Yes we dogfood FakeItEasy at my work place in large scale projects, have been doing so for over a year.Lynching
Good idea Francisco, I've created a new wiki page.Lynching
FakeItEasy looks great, but why confuse the terminology by using the term "fake" as a superset of mocks and stubs? Well known authors (Meszaros, Fowler, and others before) had already established fakes and stubs as separate concepts.Lauritz
@Roéiro, I'm not sure there was consensus about that definition, but I guess really that itäs the best superset. Ask the same question to Moq, or JMockit for that reason, they both support stubs as well and we all know that stubs and mocks are separate concepts.Lynching
H
25

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.

Hubby answered 22/10, 2010 at 21:33 Comment(5)
Lol any question with the mock or fake tag should pop up a "have you read Martin Fowler's article" pop up :PStool
That link indeed has the best definitions for these termsSadoc
Great article but man he could wor a little on formating, plain html that spans the whole screen's length, uncolored code... it really makes it hard to readStool
+1 - Martin Fowler is god, plain and simple. I have learnt a ton from his website.Labrie
Actually, no, according to Fowler's article, a "fake" is not a generic term for stubs and mocks, but rather a third kind of "test double" (there is also a fourth kind, the "dummy").Lauritz
F
12

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

Filthy answered 23/10, 2010 at 4:59 Comment(1)
Who proposed the terminology where "fake is the generic term"? I know the FakeItEasy library uses it like that, but that's it. Every other author (Fowler, Meszaros, creators of other mocking libraries) either uses "fake" for a separate kind of test double (different from both stubs and mocks), or does not use this name at all (for example, there is nothing named a "fake" in jMock, EasyMock, or Mockito).Lauritz
A
-4

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

Aphoristic answered 6/9, 2011 at 12:44 Comment(1)
You seem more interested in advertising your own project, rather than answering the question...Diphosgene

© 2022 - 2024 — McMap. All rights reserved.