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

Fake your test away: How to abuse your test dou...

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 Let’s talk about Douglas Adams Three stages in human

    civilization: • Survival • Inquiry • Sophistication
  2. @jenny_codes Let’s talk about Douglas Adams Three stages in human

    civilization: • Survival: How? • Inquiry: Why? • Sophistication: Where?
  3. @jenny_codes Let’s talk about Douglas Adams Three stages in human

    civilization: • Survival: How can we eat? • Inquiry: Why? • Sophistication: Where?
  4. @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?
  5. @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?
  6. @jenny_codes Three stages in my relationship with test doubles •

    Survival: How to use test doubles? Let’s talk about testing
  7. @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
  8. @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
  9. @jenny_codes Outline • Unit tests, integration tests, and those in

    between • Integration-test wannabes • Helicopter unit tests • How to not abuse your test doubles?
  10. @jenny_codes Outline • Unit tests, integration tests, and those in

    between • Integration-test wannabes • Helicopter unit tests • How to not abuse your test doubles?
  11. @jenny_codes Outline • Unit tests, integration tests, and those in

    between • Integration-test wannabes • Helicopter unit tests • How to not abuse your test doubles?
  12. @jenny_codes Integration-test wannabes (n.) Tests with randomly inserted test doubles

    because the real objects are too awkward to deal with.
  13. @jenny_codes Outline • Unit tests, integration tests, and those in

    between • Integration-test wannabes • Helicopter unit tests • How to not abuse your test doubles?
  14. @jenny_codes Chef Waiter Restaurant Service The dependency graph Floor 🛸

    🌝 🐢 Cupboard Fridge Bread Cheese class-level dependency instance-level dependency
  15. @jenny_codes The dependency graph 🙈 🙈 Chef Waiter Restaurant Service

    Cupboard Fridge Bread Cheese class-level dependency instance-level dependency test double 🙈 🙈 🙈
  16. @jenny_codes The dependency graph 🙈 🙈 Chef Waiter Restaurant Service

    class-level dependency instance-level dependency test double 🙈 🙈 🙈 🙈 🙈
  17. @jenny_codes Helicopter unit tests (n.) Tests that try to control

    every implementation detail in the subject.
  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?
  19. @jenny_codes • Use the Principle of Least Knowledge • 🎁

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

    • 🎁 How to not abuse your test doubles?
  21. @jenny_codes How to not abuse your test doubles? • Use

    the Principle of Least Knowledge • Separate decisions and delegations. • 🎁
  22. @jenny_codes 1. Get resources 2. Prepare ingredients 3. Make dish

    4. Serve dish => waiter => waiter => chef
  23. @jenny_codes Decision • Logic • Condition • Calculation • Unit

    test • Dependency • Relationships Delegation
  24. @jenny_codes Decision • Logic • Condition • Calculation • Unit

    test • Dependency • Relationships • Integration test Delegation
  25. @jenny_codes How to not abuse your test doubles? • Use

    the Principle of Least Knowledge • Separate decisions and delegations • 🎁
  26. @jenny_codes • Use the Principle of Least Knowledge • Separate

    decision and delegation • Make the dependencies really obvious How to not abuse your test doubles?
  27. @jenny_codes All dependencies are equal, but some are more equal

    than others. the side-e ff ect free the rest
  28. @jenny_codes All dependencies are equal, but some are more equal

    than others. the side-e ff ect free the rest 🤤
  29. @jenny_codes All dependencies are equal, but some are more equal

    than others. the side-e ff ect free the rest 🤤 ⚠
  30. @jenny_codes the side-e ff ect free 🤤 All dependencies are

    equal, but some are more equal than others. the rest ⚠ ⚠ ⚠ ⚠ ⚠ ⚠⚠⚠ ⚠
  31. @jenny_codes Chef Waiter Restaurant Service Floor 🛸 🌝 🐢 class-level

    dependency instance-level dependency Revisit serve_french_toast
  32. @jenny_codes Revisit serve_french_toast Chef Waiter Restaurant Service Floor 🛸 🌝

    🐢 class-level dependency instance-level dependency How to make those dependencies obvious?
  33. @jenny_codes • Use the Principle of Least Knowledge • Separate

    decision and delegation • Make the dependencies really obvious How to not abuse your test doubles?
  34. @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
  35. @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