Stubs Vs Mocks

  • Unit Tests should run in memory, do not interact with the external environment, e.g. file system, email system, DB etc…
  • Stubs and mocks are the same thing at birth and should be referred to as Fakes.
  •  It is when a fake is asserted against that they become a Mock.
  • IF a fake is not asserted on then it a stub and a stub cannot break your tests.
  • A test  can have multiple stubs but should only contain one Mock, as this way, if your test fails you know which mock has broken it ( as only Mocks will break a test)
  • It is possible to have different types of Fakes – strict or dynamic. A strict mock will test every eventuality within your object. Best Practice is to not use Strict as they over specify the test .e.g. your tests should only be testing one thing and that’s all. As your tests should be specific so that you can instantly tell what has been broken
Taken from Roy Osherove