PHP - Design Patterns - Test Double

php-design-patterns

What is a Test Double?

Test Double is a generic term for any case where you replace a production object for testing purposes. There are various kinds of double:

  1. Dummy: objects are passed around but never actually used. Usually they are just used to fill parameter lists.
  2. Fake: objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an InMemoryTestDatabase is a good example).
  3. 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.
  4. Spies: are stubs that also record some information based on how they were called. One form of this might be an email service that records how many messages it was sent.
  5. Mocks: are pre-programmed with expectations which form a specification of the calls they are expected to receive. They can throw an exception if they receive a call they don't expect and are checked during verification to ensure they got all the calls they were expecting.
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License