Skip to content

Instantly share code, notes, and snippets.

@nnutter
Last active October 13, 2016 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nnutter/5aaff21debdb74095c57f6435dde5906 to your computer and use it in GitHub Desktop.
Save nnutter/5aaff21debdb74095c57f6435dde5906 to your computer and use it in GitHub Desktop.
Mocking

Test Doubles

Others have defined four types of test doubles which are often colloqually referred to as mocking, mock objects, etc.

  • 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 (an in memory database is a good example).
  • 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'.
  • Mocks objects are pre-programmed with expectations which form a specification of the calls they are expected to receive.

Why use mocking?

  • A function you need to call is time-variant.
  • You want to produce an outcome that is difficult to setup, for example, a race condition.

What to avoid when using mocking?

  • Tight Coupling
    • Tests that really on mocking are likely to be testing implementation rather than behavior.
    • Changing the implementation often requires re-working the test.
    • Tests that really on mocking need to make sure that they will fail if the implementation changes in a way that bypasses
  • Global State, Spooky Action at a Distance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment