Tuesday, September 9, 2014

Unit test / Test doubles

Test doubles
"Test Double is a generic term for any case where you replace a production object for testing purposes" - Martin Fowler
  • Dummy
    • What does it do?
      • It does almost nothing. You just need it (for example) to pass as a parameter to a constructor.
    • Example?
      • A dummy class has a method which returns null.
  • Stub
    • What does it do?
      • Kind of dummy. It does a thing. It returns a specific value.
    • Example?
      • A stub class has a method which returns true.
  • Spy
    • What does it do?
      • Kind of stub. Saves any kind of things what you might need to check at the end of your test.
    • Example? 
      • A spy class saves whether its "foobar" method has been called.
  • Mock
    • What does it do?
      • Kind of spy. Does behavioural testing. It knows what behaviour is expected. What function was called, with what arguments, when and how many times.
    • Example?
      • A mock expects the "validate" method will run 2 times and will return true.
  • Fake
    • What does it do?
      • Business behavioural testing. It can return different values based on different parameters.
    •  Example?
      • A fake class has a method which returns true if the logged in user's name (a parameter) is "root" else returns false.
 
It is beneficial to read uncle Bob's article.

References
http://blog.8thlight.com/uncle-bob/2014/05/14/TheLittleMocker.html