53

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?

chillitom
  • 24,888
  • 17
  • 83
  • 118
Francisco Noriega
  • 13,725
  • 11
  • 47
  • 72
  • For a totally different kind of mocking and stubs look at the Microsoft moles framework (http://research.microsoft.com/en-us/projects/moles/). Moles allows mocking static, sealed and third party classes as well. – softveda Oct 22 '10 at 21:53

4 Answers4

109

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.

MaYaN
  • 6,683
  • 12
  • 57
  • 109
Patrik Hägne
  • 16,751
  • 5
  • 52
  • 60
  • 41
    +1 nothing beats a post by the author :-) – TheCodeJunkie Nov 13 '10 at 21:30
  • 1
    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? – Francisco Noriega Nov 13 '10 at 22:31
  • 2
    Yes we dogfood FakeItEasy at my work place in large scale projects, have been doing so for over a year. – Patrik Hägne Nov 13 '10 at 22:37
  • 1
    Good idea Francisco, I've created a new wiki page. – Patrik Hägne Nov 13 '10 at 23:00
  • 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](http://xunitpatterns.com/Mocks,%20Fakes,%20Stubs%20and%20Dummies.html) fakes and stubs as separate concepts. – Rogério Aug 25 '16 at 21:03
  • @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. – Patrik Hägne Aug 26 '16 at 08:29
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.

Adam Byrtek
  • 12,011
  • 2
  • 32
  • 32
  • 5
    Lol any question with the mock or fake tag should pop up a "have you read Martin Fowler's article" pop up :P – Francisco Noriega Oct 22 '10 at 21:36
  • 1
    That link indeed has the best definitions for these terms – softveda Oct 22 '10 at 21:52
  • 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 read – Francisco Noriega Oct 22 '10 at 22:02
  • 1
    +1 - Martin Fowler is god, plain and simple. I have learnt a ton from his website. – RPM1984 Oct 22 '10 at 23:02
  • 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"). – Rogério Aug 25 '16 at 20:32
11

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

adricadar
  • 9,971
  • 5
  • 33
  • 46
Thomas Weller
  • 11,631
  • 3
  • 26
  • 34
  • *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). – Rogério Aug 25 '16 at 20:53
-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

A J Qarshi
  • 2,772
  • 6
  • 37
  • 53
Mohamed.Radwan -MVP
  • 2,724
  • 2
  • 16
  • 24