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.

2c1dc90ff7bf69097a151677624777d2?s=128

Stuart Herbert

November 10, 2014
Tweet

Transcript

  1. 10.

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

    embedded data storage engines • Our code wasn’t multi-user When To Mock
  2. 11.

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

    embedded data storage engines • Our code wasn’t multi-user When To Mock
  3. 12.

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

    embedded data storage engines • Our code wasn’t multi-user When To Mock
  4. 20.

    @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. 21.

    @stuherbert Internally, More Complexity When To Mock Database Cache 1

    2 3 Internal Services External Services Controller Models Views Framework Libraries
  6. 22.

    @stuherbert Internally, More Dependencies When To Mock Database Cache 1

    2 3 Internal Services External Services Controller Models Views Framework Libraries Libraries Libraries
  7. 23.

    @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. 24.

    @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. 25.

    @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. 28.

    @stuherbert Still Good Advice? • Makes testing reproducible • Ease

    of use by new project members • Supports continuous integration When To Mock
  11. 29.

    @stuherbert Still Good Advice? • Makes testing reproducible • Ease

    of use by new project members • Supports continuous integration When To Mock
  12. 30.

    @stuherbert Still Good Advice? • Makes testing reproducible • Ease

    of use by new project members • Supports continuous integration When To Mock
  13. 35.

    @stuherbert Our App Now Needs These When To Mock Database

    Cache Internal Services External Services
  14. 36.

    @stuherbert If We Mock These When To Mock Database Cache

    Internal Services External Services
  15. 37.

    @stuherbert We Can Unit Test Without Them When To Mock

    Database Cache Internal Services External Services
  16. 38.

    @stuherbert & We Can Simulate Failure Too When To Mock

    Database Cache Internal Services External Services
  17. 40.

    @stuherbert • Can we mock all the things? • Should

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

    @stuherbert • Can we mock all the things? • Should

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

    @stuherbert • Can we mock all the things? • Should

    we mock all the things? • Should we mock inside our app? When To Mock
  20. 46.

    @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. 47.

    @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. 48.

    @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. 49.

    @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. 50.

    @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. 72.

    @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. 73.

    @stuherbert • How do you prove your mock behaves accurately

    today? • How do you prove your mock still behaves accurately tomorrow? When To Mock
  27. 74.

    @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. 78.

    @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. 84.

    @stuherbert Unit Tests Should Execute Straight Out Of The Box

    As Long As Test Accuracy Is Not Compromised When To Mock ✓
  30. 85.
  31. 86.

    @stuherbert Use Other Layers To Test For Things You Can’t

    Prove Because Of Your Mocks When To Mock
  32. 87.

    @stuherbert • Storyplayer for factory acceptance testing • Behat /

    BDD for product acceptance testing When To Mock