Stubs are not Mocks - Concise Version of Martin Fowler's Article

Stubs


A common helper to testing environments. There is a difference in how test results are verified: a distinction between state verification and behavior verification. Focusing on one element of the software at a time -hence the common term unit testing. The problem is that to make a single unit work, you often need other units.

Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test. Stubs may also record information about calls, such as an email gateway stub that remembers the messages it 'sent', or maybe only how many messages it 'sent'.

Dummy Objects


Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists.

Fake Objects


Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an in memory database is a good example). For a concrete example, read the article How to Use Fakes to Speed Up Tests

Mocks


Mocks are what we are talking about here: objects pre-programmed with expectations which form a specification of the calls they are expected to receive.

Only mocks insist upon behavior verification. The other doubles can, and usually do, use state verification. For a concrete example, read the article on Fake and Mocks

Discussion


The classical TDD style is to use real objects if possible and a double if it's awkward to use the real thing. A mockist TDD practitioner, however, will always use a mock for any object with interesting behavior.

An acknowledged issue with state-based verification is that it can lead to creating query methods only to support verification. It's never comfortable to add methods to the API of an object purely for testing, using behavior verification avoids that problem.


Related Articles


Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.