Fakes, Mocks, and Stubs, What’s the Difference?

It doesn’t take much unit testing before you come across a dependency in the class you are testing for another object. Hopefully your classes are loosely coupled and you are able to make a substitution for this other object. It seems most people call these substitute objects mocks but a mock is a specific thing. According to Martin Fowler there are 4 types of substitution:

  • Dummy – An object that is never actually used, just required to fill out the parameter list.
  • Fake – An object with just enough of a working implementation to substitute.
  • Stub – An object that can provide canned answers to member calls and, optionally, record the calls for later interrogation.
  • Mock – An object that is programmed with a set of expectations of which calls will be made, in what order with what parameters. It is the expectation that is tested after the code has been exercised.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.