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 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s