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

Fake your test away: How to abuse your test doubles

Jenny Shih
November 11, 2021

Fake your test away: How to abuse your test doubles

Presented at RubyConf 2021 by Jenny Shih

https://rubyconf.com/program/sessions#session-1162

Jenny Shih

November 11, 2021
Tweet

More Decks by Jenny Shih

Other Decks in Programming

Transcript

  1. @jenny_codes
    Fake your test away
    How to abuse your test doubles

    View full-size slide

  2. @jenny_codes

    View full-size slide

  3. @jenny_codes
    @ RubyConf Taiwan 2019

    View full-size slide

  4. @jenny_codes
    @ RubyConf Taiwan 2019

    View full-size slide

  5. @jenny_codes
    @ RubyConf Taiwan 2019

    View full-size slide

  6. @jenny_codes
    Let’s talk about Douglas Adams

    View full-size slide

  7. @jenny_codes
    Let’s talk about Douglas Adams
    source: https://www.wallpaper
    fl
    are.com/don-t-panic-text-decor-the-hitchhiker-s-guide-to-the-galaxy-wallpaper-pwpgv

    View full-size slide

  8. @jenny_codes
    Let’s talk about Douglas Adams
    Three stages in human civilization:


    • Survival


    • Inquiry


    • Sophistication

    View full-size slide

  9. @jenny_codes
    Let’s talk about Douglas Adams
    Three stages in human civilization:


    • Survival: How?


    • Inquiry: Why?


    • Sophistication: Where?

    View full-size slide

  10. @jenny_codes
    Let’s talk about Douglas Adams
    Three stages in human civilization:


    • Survival: How can we eat?


    • Inquiry: Why?


    • Sophistication: Where?

    View full-size slide

  11. @jenny_codes
    Let’s talk about Douglas Adams
    Three stages in human civilization:


    • Survival: How can we eat?


    • Inquiry: Why do we eat?


    • Sophistication: Where?

    View full-size slide

  12. @jenny_codes
    Let’s talk about Douglas Adams
    Three stages in human civilization:


    • Survival: How can we eat?


    • Inquiry: Why do we eat?


    • Sophistication: Where shall we have lunch?

    View full-size slide

  13. @jenny_codes
    Let’s talk about testing

    View full-size slide

  14. @jenny_codes
    Three stages in my relationship with test doubles:
    Let’s talk about testing

    View full-size slide

  15. @jenny_codes
    Three stages in my relationship with test doubles


    • Survival: How to use test doubles?


    Let’s talk about testing

    View full-size slide

  16. @jenny_codes
    Three stages in my relationship with test doubles


    • Survival: How to use test doubles?


    • Inquiry: Why do I test?


    Let’s talk about testing

    View full-size slide

  17. @jenny_codes
    Three stages in my relationship with test doubles


    • Survival: How to use test doubles?


    • Inquiry: Why do I test?


    • Sophistication: Where shall I begin to delete my old tests?


    Let’s talk about testing

    View full-size slide

  18. @jenny_codes
    Outline
    • Unit tests, integration tests, and those in between


    • Integration-test wannabes


    • Helicopter unit tests


    • How to not abuse your test doubles?

    View full-size slide

  19. @jenny_codes
    Outline
    • Unit tests, integration tests, and those in between


    • Integration-test wannabes


    • Helicopter unit tests


    • How to not abuse your test doubles?

    View full-size slide

  20. @jenny_codes
    Test double

    View full-size slide

  21. @jenny_codes
    Test double


    ├─ Stub


    ├─ Mock


    └─ Spy

    View full-size slide

  22. @jenny_codes
    Subject

    View full-size slide

  23. @jenny_codes
    Dependency

    View full-size slide

  24. Imagine a perfect world

    View full-size slide

  25. @jenny_codes
    Testing pyramid
    Unit tests
    Integration
    tests
    Acceptance
    tests

    View full-size slide

  26. @jenny_codes
    Testing pyramid, reality edition
    Integration-test
    wannabes
    Helicopter
    unit tests
    Scream tests
    🤔 🤔
    🤔
    🤔

    View full-size slide

  27. @jenny_codes
    Testing pyramid, reality edition
    Helicopter
    unit tests
    Scream tests
    🤔 🤔
    🤔
    🤔
    Integration-test
    wannabes

    View full-size slide

  28. @jenny_codes
    Testing pyramid, reality edition
    Helicopter
    unit tests
    Scream tests
    🤔 🤔
    🤔
    🤔
    Integration-test
    wannabes

    View full-size slide

  29. @jenny_codes
    Testing pyramid, reality edition
    Helicopter
    unit tests
    Scream tests
    🤔 🤔
    🤔
    🤔
    Integration-test
    wannabes

    View full-size slide

  30. @jenny_codes
    Outline
    • Unit tests, integration tests, and those in between


    • Integration-test wannabes


    • Helicopter unit tests


    • How to not abuse your test doubles?

    View full-size slide

  31. @jenny_codes
    A restaurant that serves French toast

    View full-size slide

  32. @jenny_codes
    A restaurant that serves French toast

    View full-size slide

  33. @jenny_codes
    A restaurant that serves French toast

    View full-size slide

  34. @jenny_codes
    A restaurant that serves French toast

    View full-size slide

  35. @jenny_codes
    A restaurant that serves French toast

    View full-size slide

  36. @jenny_codes
    The test

    View full-size slide

  37. @jenny_codes
    The test

    View full-size slide

  38. @jenny_codes
    🤔
    The test

    View full-size slide

  39. @jenny_codes

    View full-size slide

  40. @jenny_codes
    🔎

    View full-size slide

  41. @jenny_codes
    🔎

    View full-size slide

  42. @jenny_codes
    🔎
    🚨

    View full-size slide

  43. @jenny_codes

    View full-size slide

  44. @jenny_codes

    View full-size slide

  45. @jenny_codes

    View full-size slide

  46. @jenny_codes

    View full-size slide

  47. @jenny_codes

    View full-size slide

  48. @jenny_codes

    View full-size slide

  49. @jenny_codes

    View full-size slide

  50. @jenny_codes

    View full-size slide

  51. @jenny_codes

    View full-size slide

  52. @jenny_codes

    View full-size slide

  53. @jenny_codes

    View full-size slide

  54. @jenny_codes

    View full-size slide

  55. @jenny_codes
    Integration-test wannabes (n.)
    Tests with randomly inserted test doubles because the real objects are
    too awkward to deal with.

    View full-size slide

  56. @jenny_codes
    ❌ Random doubles == Random test failures
    Integration-test wannabes

    View full-size slide

  57. @jenny_codes
    One month later…

    View full-size slide

  58. @jenny_codes
    One month later…
    FAILED

    View full-size slide

  59. @jenny_codes
    Therapeutic Refactoring by Katrina Owen, Cascadia Ruby Conf 2012

    View full-size slide

  60. @jenny_codes

    View full-size slide

  61. @jenny_codes
    when I don’t have to modify
    100 random test failures for it

    View full-size slide

  62. @jenny_codes
    ❌ Random doubles == Random test failures


    ❌ Decreased productivity
    Integration-test wannabes

    View full-size slide

  63. Test doubles are contracts

    View full-size slide

  64. @jenny_codes
    Outline
    • Unit tests, integration tests, and those in between


    • Integration-test wannabes


    • Helicopter unit tests


    • How to not abuse your test doubles?

    View full-size slide

  65. @jenny_codes
    Restaurant Service: Cheese Panini

    View full-size slide

  66. @jenny_codes

    View full-size slide

  67. @jenny_codes

    View full-size slide

  68. @jenny_codes

    View full-size slide

  69. @jenny_codes

    View full-size slide

  70. @jenny_codes
    Chef
    Waiter
    Restaurant


    Service
    The dependency graph
    Floor
    🛸
    🌝
    🐢
    Cupboard
    Fridge
    Bread
    Cheese
    class-level dependency
    instance-level dependency

    View full-size slide

  71. @jenny_codes
    The dependency graph
    🙈
    🙈
    Chef
    Waiter
    Restaurant


    Service
    Cupboard
    Fridge
    Bread
    Cheese
    class-level dependency
    instance-level dependency
    test double
    🙈
    🙈
    🙈

    View full-size slide

  72. @jenny_codes
    The dependency graph
    🙈
    🙈
    Chef
    Waiter
    Restaurant


    Service
    class-level dependency
    instance-level dependency
    test double
    🙈
    🙈
    🙈
    🙈
    🙈

    View full-size slide

  73. @jenny_codes

    View full-size slide

  74. @jenny_codes

    View full-size slide

  75. @jenny_codes

    View full-size slide

  76. @jenny_codes

    View full-size slide

  77. @jenny_codes

    View full-size slide

  78. @jenny_codes

    View full-size slide

  79. @jenny_codes

    View full-size slide

  80. @jenny_codes

    View full-size slide

  81. @jenny_codes
    Helicopter unit tests (n.)
    Tests that try to control every implementation detail in the subject.

    View full-size slide

  82. @jenny_codes
    ❌ Fake empire
    The helicopter unit tests

    View full-size slide

  83. @jenny_codes https://media.giphy.com/media/IguL9zRYV20X1QeKuV/giphy.gif
    Production
    Your CI
    The helicopter unit tests

    View full-size slide

  84. @jenny_codes
    ❌ Fake empire


    ❌ Brittle tests
    The helicopter unit tests

    View full-size slide

  85. @jenny_codes
    ❌ Fake empire


    ❌ Brittle tests
    The helicopter unit tests
    GUARANTEED

    View full-size slide

  86. @jenny_codes
    ❌ Fake empire


    ❌ Brittle tests


    The helicopter unit tests
    x N

    View full-size slide

  87. @jenny_codes
    Outline
    • Unit tests, integration tests, and those in between


    • Integration-test wannabes


    • Helicopter unit tests


    • How to not abuse your test doubles?

    View full-size slide

  88. @jenny_codes
    hard-to-use code == hard-to-write test
    easy-to-use code == easy-to-write test

    View full-size slide

  89. @jenny_codes
    Tests are the first consumer of your API

    View full-size slide

  90. @jenny_codes
    https://twitter.com/dan_abramov/status/1456265490754510848/photo/1
    Write an unit test
    that can’t catch errors
    Write an integration test
    that has holes it

    View full-size slide

  91. @jenny_codes
    • 🎁


    • 🎁


    • 🎁
    How to not abuse your test doubles?

    View full-size slide

  92. @jenny_codes
    • Use the Principle of Least Knowledge


    • 🎁


    • 🎁
    How to not abuse your test doubles?

    View full-size slide

  93. @jenny_codes
    Principle of Least Knowledge
    Don’t talk to strangers

    View full-size slide

  94. @jenny_codes
    Principle of Least Knowledge

    View full-size slide

  95. @jenny_codes
    Principle of Least Knowledge
    Life Universe Everything
    Answer

    View full-size slide

  96. @jenny_codes
    Principle of Least Knowledge
    Life Universe Everything
    Answer

    View full-size slide

  97. @jenny_codes
    Principle of Least Knowledge
    Life Universe
    Answer
    Everything

    View full-size slide

  98. @jenny_codes
    Principle of Least Knowledge
    Answer
    Everything
    Universe
    Life

    View full-size slide

  99. @jenny_codes
    Principle of Least Knowledge
    Don’t talk to strangers


    Low coupling

    View full-size slide

  100. @jenny_codes
    Principle of Least Knowledge
    Don’t talk to strangers


    Low coupling

    View full-size slide

  101. @jenny_codes
    Principle of Least Knowledge
    Don’t talk to strangers


    Low coupling

    View full-size slide

  102. @jenny_codes

    View full-size slide

  103. @jenny_codes

    View full-size slide

  104. @jenny_codes

    View full-size slide

  105. @jenny_codes

    View full-size slide

  106. @jenny_codes

    View full-size slide

  107. @jenny_codes

    View full-size slide

  108. @jenny_codes
    Use Principle of Least Knowledge: Result

    View full-size slide

  109. @jenny_codes
    Use Principle of Least Knowledge: Result

    View full-size slide

  110. @jenny_codes
    • Use the Principle of Least Knowledge


    • 🎁


    • 🎁
    How to not abuse your test doubles?

    View full-size slide

  111. @jenny_codes
    How to not abuse your test doubles?
    • Use the Principle of Least Knowledge


    • Separate decisions and delegations.


    • 🎁

    View full-size slide

  112. @jenny_codes
    Decision
    • Logic


    • Condition


    • Calculation
    • Dependency


    • Relationships
    Delegation

    View full-size slide

  113. @jenny_codes

    View full-size slide

  114. @jenny_codes
    = delegation
    = decision

    View full-size slide

  115. @jenny_codes
    1. Get resources


    2. Prepare ingredients


    3. Make dish


    4. Serve dish

    View full-size slide

  116. @jenny_codes
    1. Get resources

    View full-size slide

  117. @jenny_codes
    1. Get resources


    2. Prepare ingredients


    3. Make dish


    4. Serve dish

    View full-size slide

  118. @jenny_codes
    1. Get resources


    2. Prepare ingredients


    3. Make dish


    4. Serve dish
    => waiter
    => waiter
    => chef

    View full-size slide

  119. @jenny_codes
    Separate decision and delegation: Result

    View full-size slide

  120. @jenny_codes
    Separate decision and delegation: Result

    View full-size slide

  121. @jenny_codes

    View full-size slide

  122. @jenny_codes

    View full-size slide

  123. @jenny_codes

    View full-size slide

  124. @jenny_codes
    Separate decision and delegation: Result
    Arrange
    Act
    Assert

    View full-size slide

  125. @jenny_codes
    Separate decision and delegation: Result

    View full-size slide

  126. @jenny_codes
    Decision
    • Logic


    • Condition


    • Calculation


    • Unit test
    • Dependency


    • Relationships
    Delegation

    View full-size slide

  127. @jenny_codes
    Decision
    • Logic


    • Condition


    • Calculation


    • Unit test
    • Dependency


    • Relationships


    • Integration test
    Delegation

    View full-size slide

  128. @jenny_codes
    Testing pyramid with decision and delegation
    Unit tests
    Integration
    tests
    Acceptance
    tests
    Decisions
    Delegation

    View full-size slide

  129. @jenny_codes
    How to not abuse your test doubles?
    • Use the Principle of Least Knowledge


    • Separate decisions and delegations


    • 🎁

    View full-size slide

  130. @jenny_codes
    • Use the Principle of Least Knowledge


    • Separate decision and delegation


    • Make the dependencies really obvious
    How to not abuse your test doubles?

    View full-size slide

  131. @jenny_codes
    All dependencies are equal,


    but some are more equal than others.

    View full-size slide

  132. @jenny_codes
    All dependencies are equal,


    but some are more equal than others.
    the side-e
    ff
    ect free the rest

    View full-size slide

  133. @jenny_codes
    All dependencies are equal,


    but some are more equal than others.
    the side-e
    ff
    ect free the rest
    🤤

    View full-size slide

  134. @jenny_codes
    All dependencies are equal,


    but some are more equal than others.
    the side-e
    ff
    ect free the rest
    🤤 ⚠

    View full-size slide

  135. @jenny_codes
    the side-e
    ff
    ect free
    🤤
    All dependencies are equal,


    but some are more equal than others.
    the rest





    ⚠⚠⚠

    View full-size slide

  136. @jenny_codes
    Revisit serve_french_toast

    View full-size slide

  137. @jenny_codes
    Chef
    Waiter
    Restaurant


    Service
    Floor
    🛸
    🌝
    🐢
    class-level dependency
    instance-level dependency
    Revisit serve_french_toast

    View full-size slide

  138. @jenny_codes
    Revisit serve_french_toast
    Chef
    Waiter
    Restaurant


    Service
    Floor
    🛸
    🌝
    🐢
    class-level dependency
    instance-level dependency

    View full-size slide

  139. @jenny_codes
    Revisit serve_french_toast
    Chef
    Waiter
    Restaurant


    Service
    Floor
    🛸
    🌝
    🐢
    class-level dependency
    instance-level dependency

    View full-size slide

  140. @jenny_codes
    Revisit serve_french_toast
    Chef
    Waiter
    Restaurant


    Service
    Floor
    🛸
    🌝
    🐢
    class-level dependency
    instance-level dependency

    View full-size slide

  141. @jenny_codes
    Revisit serve_french_toast
    Chef
    Waiter
    Restaurant


    Service
    Floor
    🛸
    🌝
    🐢
    class-level dependency
    instance-level dependency
    How to make those
    dependencies obvious?

    View full-size slide

  142. @jenny_codes
    Revisit serve_french_toast
    Chef
    Waiter
    Restaurant


    Service
    Floor
    🛸
    🌝
    🐢
    class-level dependency
    instance-level dependency

    View full-size slide

  143. @jenny_codes
    Chef
    Waiter
    Restaurant


    Service
    Floor
    🛸 🌝 🐢

    View full-size slide

  144. @jenny_codes
    Chef
    Waiter
    Restaurant


    Service
    Floor
    🛸
    🌝
    🐢

    View full-size slide

  145. @jenny_codes
    Chef
    Waiter
    Restaurant


    Service
    Floor
    🛸
    🌝
    🔻
    🐢
    Restaurant


    Repo

    View full-size slide

  146. @jenny_codes
    Chef
    Waiter
    Restaurant


    Service
    Floor
    🛸
    🌝
    🔻
    🐢
    Restaurant


    Repo

    View full-size slide

  147. @jenny_codes

    View full-size slide

  148. @jenny_codes



    View full-size slide

  149. @jenny_codes

    View full-size slide

  150. @jenny_codes

    View full-size slide

  151. @jenny_codes
    Make dependencies obvious: Result

    View full-size slide

  152. @jenny_codes
    Make dependencies obvious: Result

    View full-size slide

  153. @jenny_codes
    Make dependencies obvious: Result

    View full-size slide

  154. @jenny_codes
    • Use the Principle of Least Knowledge


    • Separate decision and delegation


    • Make the dependencies really obvious
    How to not abuse your test doubles?

    View full-size slide

  155. @jenny_codes
    Design your system


    so there’s no room to abuse your test doubles

    View full-size slide

  156. One last thing

    View full-size slide

  157. @jenny_codes
    Talks that inspire me
    • Gary Bernhardt: Boundaries


    • Justin Searls: Please don’t mock me


    • Justin Searls: How to stop hating your test


    • Katrina Owen: Therapeutic refactoring


    • All the talks by Sandi Metz really

    View full-size slide

  158. @jenny_codes
    Books that inspire me
    • The Art of Unit Testing by Roy Osherove


    • Domain Modeling Made Functional by Scott Wlaschin


    • The Hitchhiker’s Guide to the Galaxy by Douglas Adams

    View full-size slide

  159. @jenny_codes
    Talk to me!
    or just
    @jenny_codes

    View full-size slide