Monday, May 08, 2006

Test Doubles (via Martin Fowler)

Martin Fowler writes "Gerard Meszaros is working on a book to capture patterns for using the various Xunit frameworks". This looks very interesting and I intend to peruse it more when I have free-time, but for now I'd just like to repeat the little snippet of definitions that Martin included.

Test Double is a generic term for any case where you replace a production object for testing purposes. There are various kinds of double that Gerard lists:
  • 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 InMemoryDatabase 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 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.

0 comments: