January 8, 2007
Ned recently pointed out Martin Fowler's article Mocks Aren't Stubs.
The vocabulary for talking about [pretend objects] soon gets messy - all sorts of words are used: stub, mock, fake, dummy. For this article I'm going to follow the vocabulary of Gerard Meszaros's upcoming book. It's not what everyone uses, but I think it's a good vocabulary and since it's my essay I get to pick which words to use. Meszaros uses the term Test Double as the generic term for any kind of pretend object used in place of a real object for testing purposes.
Personally, I like to think of them as Stunt Objects. Meszaros, via Fowler, defines the following taxonomy of pretend objects:
Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists.
Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production.
Stub objects provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test.
Mock objects are pre-programmed with expectations which form a specification of the calls they are expected to receive.
Posted by Jeff Atwood
Admit it, you just wanted to post a picture of boobies.
Marcus, I use mocks almost exclusively when writing my unit tests because if you attempt to have the thing you are unit testing use actual objects it quickly becomes a (complicated) systems test instead. Mocks can also let you do some tests that are otherwise hard-to-impossible to test, such as asserting that your object under unit test only makes some call once (this is useful where that call is possibly long-running or state-modifying in some way that is hard to test).
If I'm not mistaken, I believe that picture comes from the Victoria's Secret catalog. October 2005 edition. Page 27.
And boobies dominate over ood stuff. I'm confident about fate of this world :).
As i understand it, mock objects are usually used in unit testing situations: a unit test should have as few dependencies as possible, ideally it should only depend on the object/method that is being tested.
The issue is that objects are very often dependent on one another (composed with other objects, ...), and your unit tests therefore end up testing what you want to test, but also every object in the composition hierarchy of what you want to test.
Mock objects are used to remove this composition hierarchy by composing the tested object with mock objects, therefore removing all dependencies, and allowing you to test only what you want to test, and nothing else.
They look to me like they're ready for production.
Marcus, it is good to take a look at the original mock objects paper too: (pdf) http://www.connextra.com/aboutUs/mockobjects.pdf. As I understand them, mock objects are like little spies that can be inserted into other objects to check whether they are being used as intended. They also have the good property of breaking dependencies and can also work as stubs providing canned data.
Great picture choices. Insert here appropriate or inappropriate witty remark here about the silicone twins.
I really love the choice for 'mock'. 1) It's a perfect choice. I really can't think of anything that would have been as good. 2) I'm an avid Lewis Carroll and John Tenniel fan and love seeing the Alice artwork get recognition.
Mock, dummy, fake .. what is this. Name your stub category day? They all STUBS. Why make it more confusing?
Look I just created a dummy object that morphs into a fake when stubed in my fake test...
I was hopping to pop over here and make some clever comment about the imagery used in this post. However, it looks like any sophmoric quip I could have come up with has already been covered. Excellent job, gents.
had a hard time explaining to my female co-worker at office that I was reading a technical blog. Thanks.
I'm with dinah. It filled my heart with joy to see the mock turtle.
What I want to know is what is that piece of hay/grass sticking down in the boob shot on the right? It looks like the lady has something in her mouth and you cropped it all out. If you haven't done any cropping then it is a complete mystery as to why this would be there in the first place. Please elaborate!
Caprice Bourret. I'd recognize those anywhere.
Matt, I'm pretty sure that's a strand of hair, not hay/grass.
It was difficult to come up with a better image choice for "Fake". "Mock", on the other hand, is incredibly difficult.
I never really understood what the whole "mocking" business is all about. Why not just implement the objects to begin with?
How hard was it to find a picture of boobs that actually had something covering them?
Thanks for the pointers you guys!
I wanted to show a co-worker the funny Ping video game from a previous post, but I nearly had a panic attack when the "Fake" picture popped onto his screen. I think your blog is excellent, and I appreciate the feminine form as much as any man, but PLEASE don't put things on your site that might get me fired.
Jesus. it's a pair of covered tits. It's not like you are browsing pr0n. If you get fired for that small amount of skin above you should be glad to be out and not working for such an uptight employer.
Maybe you shouldn't be reading Coding Horror at work anyway. That's why they call it "work" and not "play time".
Haven't you read the "study" showing that people who read tech blogs are more productive and better programmers (or whatever it is we do)?
Perfect justification for reading blogs at work, I'd say. It's "professional development."
Regarding cland's comment:
The category is "testing doubles" or "substitutes", not "stubs".
I agree with the taxonomy completely, but would propose one more:
Sometimes your test double derives from the actual object under test.
I propose calling these "self mocks", "self stubs", etc.
In general when you find yourself doing this it indicates other problems in your code. However, it is sometimes necessary for the purposes of making something visible, or for overriding methods to substitute only part of the object.
it's funny how terms take hold. it's unfortunate in some sense.
for instance, the term 'stub' barely has any semantic relevance. at best it would be as a receipt of transaction which we could take to mean recording the method calls, but this is becoming known as a test 'spy'. or is it that the stub stands in for the check? honestly i haven't slightest why 'stub' was every the selected term.
then there is the english meaning of 'mock' which really conveys nothing about verification, but only the idea of something that pretends to be something else. in that sense all test doubles are mocks, even if ever so scantily. left to my intuition i'd expect a mock to be what dave mann called a 'self mock'. and for mock, something like 'appraiser' --you know, someone with a checklist, seems more fitting.
is it valuable to use terms with general meanings more akin to a specialized use? i think so. it saves brain cells. unfortunately I think we love out DD-style realm building too much to actually do so.
Seriously. Covered or not, I'm yet another person who reads this blog at work and would have a hard time explaining that one off.
Elf/Vulcan ears or cat-eye contacts or an obvious toupee or an obvious nose-job -- anything else would be more SFW.
Just another $0.02. Thanks for the (in general) all around fantastic blog. :)
Thanks for the taxonomy. It certainly clarifies what (some) people mean when they talk about such things.
I do not feel a day-to-day need to make such fine distinctions between dummy, fake, stub and mock objects - I know what I need when I need it - but I applaud you for doing it for me.
Frankly, I rarely find the need to make such fine distinctions between the different types of "testing" objects identified but it will make reading Mr Fowler's articles a lot clearer.