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.

2c1dc90ff7bf69097a151677624777d2?s=128

Stuart Herbert

November 10, 2014
Tweet

Transcript

  1. @stuherbert When To Mock

  2. @stuherbert Introductions When To Mock

  3. @stuherbert of Unit Testing The Holy Grail When To Mock

  4. @stuherbert When To Mock $ git clone git@github.com:foo/bar.git $ cd

    bar $ composer install $ phpunit
  5. @stuherbert When To Mock $ git clone git@github.com:foo/bar.git $ cd

    bar $ composer install $ phpunit
  6. @stuherbert When To Mock $ git clone git@github.com:foo/bar.git $ cd

    bar $ composer install $ phpunit
  7. @stuherbert When To Mock $ git clone git@github.com:foo/bar.git $ cd

    bar $ composer install $ phpunit
  8. @stuherbert Unit Tests Should Execute Straight Out Of The Box

    When To Mock
  9. @stuherbert This Was Easy In The Pre-Web World When To

    Mock
  10. @stuherbert • Our code wasn’t networked • Our code used

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

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

    embedded data storage engines • Our code wasn’t multi-user When To Mock
  13. @stuherbert Self-Contained Our Code Was When To Mock

  14. @stuherbert Pre-Web Architecture Diagram When To Mock

  15. @stuherbert Pre-Web Architecture Diagram When To Mock

  16. @stuherbert Pre-Web Architecture Diagram When To Mock Application Stack

  17. @stuherbert Talks To Things Today’s Code When To Mock

  18. @stuherbert Self Contained Today’s Code Is Anything But When To

    Mock
  19. @stuherbert Our Code Has Evolved From This When To Mock

    Application Stack
  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
  21. @stuherbert Internally, More Complexity When To Mock Database Cache 1

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

    2 3 Internal Services External Services Controller Models Views Framework Libraries Libraries Libraries
  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
  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
  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
  26. @stuherbert Unit Tests Should Execute Straight Out Of The Box

    When To Mock
  27. @stuherbert Good Advice? Is This Still When To Mock

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

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

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

    of use by new project members • Supports continuous integration When To Mock
  31. @stuherbert Unit Tests Should Execute Straight Out Of The Box

    When To Mock ✓
  32. @stuherbert Now Requires Help Making Unit Tests Execute Out Of

    The Box When To Mock
  33. @stuherbert We Want To Unit Test Like This When To

    Mock Application Stack
  34. @stuherbert Are Gone But Those Days When To Mock

  35. @stuherbert Our App Now Needs These When To Mock Database

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

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

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

    Database Cache Internal Services External Services
  39. @stuherbert Three Key Questions When To Mock

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

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

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

    we mock all the things? • Should we mock inside our app? When To Mock
  43. @stuherbert All The Things? Can We Mock When To Mock

  44. @stuherbert • Given enough time and effort, yes we can

    When To Mock
  45. @stuherbert All The Things? Should We Mock When To Mock

  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
  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
  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
  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
  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
  51. @stuherbert Test what we can, mock what we have to

    When To Mock
  52. @stuherbert Inside Our App? Should We Mock When To Mock

  53. @stuherbert Our Code? What Happens Inside When To Mock

  54. @stuherbert • computation • branching • input / output When

    To Mock
  55. @stuherbert • computation • branching • input / output When

    To Mock
  56. @stuherbert • computation • branching • input / output When

    To Mock
  57. @stuherbert Forming Execution Paths When To Mock

  58. @stuherbert One Execution Path Each Unit Test Exercises When To

    Mock
  59. @stuherbert Such As This When To Mock

  60. @stuherbert Or This When To Mock

  61. @stuherbert Or This When To Mock

  62. @stuherbert The Code You Call? What Happens When You Mock

    When To Mock
  63. @stuherbert Adding Mocks Shortens The Paths We Can Test When

    To Mock
  64. @stuherbert Take A Code Path When To Mock

  65. @stuherbert Inject A Mock When To Mock

  66. @stuherbert Shorter Code Path When To Mock

  67. @stuherbert Behind The Mock? But What About The Code When

    To Mock
  68. @stuherbert ? Unreachable Code When To Mock ? ? ?

  69. @stuherbert Unreachable Code Mocks Inside Your App Create When To

    Mock
  70. @stuherbert Is Untestable Code Unreachable Code When To Mock

  71. @stuherbert What Is The Risk From The Code That You

    Can’t Test? When To Mock
  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
  73. @stuherbert • How do you prove your mock behaves accurately

    today? • How do you prove your mock still behaves accurately tomorrow? When To Mock
  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
  75. @stuherbert The Best Approach Mocks Are Sometimes When To Mock

  76. @stuherbert Why? When To Mock

  77. @stuherbert Is Reachable Not Every Code Path When To Mock

  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
  79. @stuherbert To Test For Failure Mocks Are A Great Way

    When To Mock
  80. @stuherbert In Summary When To Mock

  81. @stuherbert Test what we can, mock what we have to

    When To Mock
  82. @stuherbert Unit Tests Should Execute Straight Out Of The Box

    When To Mock
  83. @stuherbert Unit Tests Should Execute Straight Out Of The Box

    When To Mock ✗
  84. @stuherbert Unit Tests Should Execute Straight Out Of The Box

    As Long As Test Accuracy Is Not Compromised When To Mock ✓
  85. @stuherbert Unit Tests Should Be One Of Several Layers In

    Your Test Strategy When To Mock
  86. @stuherbert Use Other Layers To Test For Things You Can’t

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

    BDD for product acceptance testing When To Mock
  88. @stuherbert Thank You Any Questions? When To Mock