Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are fakes better than Mocks? [closed]

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?

like image 913
Francisco Noriega Avatar asked Oct 22 '10 21:10

Francisco Noriega


People also ask

What is the difference between mocks stubs and fakes?

Mocks and stubs can be hand generated or generated by a mocking framework. Fake classes are generated by hand. I use mocks primarily to verify interactions between my class and dependent classes. I use stubs once I have verified the interactions and am testing alternate paths through my code.

What are fakes in testing?

Fake. Fakes are objects that have working implementations, but not same as production one. Usually they take some shortcut and have simplified version of production code.

What starts out as a fake until it's asserted against?

A mock starts out as a Fake until it's asserted against.

What is the difference between mock and spy?

Mocks are used to create fully mock or dummy objects. It is mainly used in large test suites. Spies are used for creating partial or half mock objects. Like mock, spies are also used in large test suites.


2 Answers

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.

like image 54
Patrik Hägne Avatar answered Oct 08 '22 23:10

Patrik Hägne


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.

like image 24
Adam Byrtek Avatar answered Oct 09 '22 00:10

Adam Byrtek