Upgrade to Pro — share decks privately, control downloads, hide ads and more …

When To Mock

When To Mock

What are mocks, what are the consequences of using them, and when should you use them?

Slides from my talk @PHPDorset on 2014/11/03.

Stuart Herbert

November 10, 2014
Tweet

More Decks by Stuart Herbert

Other Decks in Programming

Transcript

  1. @stuherbert • Our code wasn’t networked • Our code used

    embedded data storage engines • Our code wasn’t multi-user When To Mock
  2. @stuherbert • Our code wasn’t networked • Our code used

    embedded data storage engines • Our code wasn’t multi-user When To Mock
  3. @stuherbert • Our code wasn’t networked • Our code used

    embedded data storage engines • Our code wasn’t multi-user When To Mock
  4. @stuherbert Our Code Now Looks Like This When To Mock

    Database Cache 1 2 3 Internal Services External Services Controller Models Views Framework Libraries Libraries Libraries
  5. @stuherbert Internally, More Complexity When To Mock Database Cache 1

    2 3 Internal Services External Services Controller Models Views Framework Libraries
  6. @stuherbert Internally, More Dependencies When To Mock Database Cache 1

    2 3 Internal Services External Services Controller Models Views Framework Libraries Libraries Libraries
  7. @stuherbert Storage Is Now A Service When To Mock Database

    Cache 1 2 3 Internal Services External Services Controller Models Views Framework Libraries Libraries Libraries
  8. @stuherbert Functionality Is Now A Service When To Mock Database

    Cache 1 2 3 Internal Services External Services Controller Models Views Framework Libraries Libraries Libraries
  9. @stuherbert Apps Are Now A Service To Users When To

    Mock Database Cache 1 2 3 Internal Services External Services Controller Models Views Framework Libraries Libraries Libraries
  10. @stuherbert Still Good Advice? • Makes testing reproducible • Ease

    of use by new project members • Supports continuous integration When To Mock
  11. @stuherbert Still Good Advice? • Makes testing reproducible • Ease

    of use by new project members • Supports continuous integration When To Mock
  12. @stuherbert Still Good Advice? • Makes testing reproducible • Ease

    of use by new project members • Supports continuous integration When To Mock
  13. @stuherbert Our App Now Needs These When To Mock Database

    Cache Internal Services External Services
  14. @stuherbert If We Mock These When To Mock Database Cache

    Internal Services External Services
  15. @stuherbert We Can Unit Test Without Them When To Mock

    Database Cache Internal Services External Services
  16. @stuherbert & We Can Simulate Failure Too When To Mock

    Database Cache Internal Services External Services
  17. @stuherbert • Can we mock all the things? • Should

    we mock all the things? • Should we mock inside our app? When To Mock
  18. @stuherbert • Can we mock all the things? • Should

    we mock all the things? • Should we mock inside our app? When To Mock
  19. @stuherbert • Can we mock all the things? • Should

    we mock all the things? • Should we mock inside our app? When To Mock
  20. @stuherbert • Who is going to build the mocks? •

    How do you prove your mock behaves accurately today? • How do you prove your mock still behaves accurately tomorrow? When To Mock
  21. @stuherbert • Who is going to build the mocks? •

    How do you prove your mock behaves accurately today? • How do you prove your mock still behaves accurately tomorrow? When To Mock
  22. @stuherbert • Who is going to build the mocks? •

    How do you prove your mock behaves accurately today? • How do you prove your mock still behaves accurately tomorrow? When To Mock
  23. @stuherbert • A mock can only be as good as

    the author’s understanding of whatever is being mocked • Tests using rotted mocks will continue to pass, but the code will fail when shipped When To Mock
  24. @stuherbert • A mock can only be as good as

    the author’s understanding of whatever is being mocked • Tests using rotted mocks will continue to pass, but the code will fail when shipped When To Mock
  25. @stuherbert Key Questions • How do you prove your mock

    behaves accurately today? • How do you prove your mock still behaves accurately tomorrow? When To Mock
  26. @stuherbert • How do you prove your mock behaves accurately

    today? • How do you prove your mock still behaves accurately tomorrow? When To Mock
  27. @stuherbert • A mock can only be as good as

    the author’s understanding of whatever is being mocked • Tests using rotted mocks will continue to pass, but the code will fail when shipped When To Mock
  28. @stuherbert Testing For Failure • How do you get bad

    responses from the code you call? • How do you trigger your error handling? When To Mock
  29. @stuherbert Unit Tests Should Execute Straight Out Of The Box

    As Long As Test Accuracy Is Not Compromised When To Mock ✓
  30. @stuherbert Use Other Layers To Test For Things You Can’t

    Prove Because Of Your Mocks When To Mock
  31. @stuherbert • Storyplayer for factory acceptance testing • Behat /

    BDD for product acceptance testing When To Mock