Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Fake your test away: How to abuse your test dou...
Search
Jenny Shih
November 11, 2021
Programming
0
180
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
Share
More Decks by Jenny Shih
See All by Jenny Shih
Functional Programming for Fun and Profit!!
jennyshih
0
750
Context-Driven Development: Architecture your code with Phoenix context
jennyshih
0
890
Other Decks in Programming
See All in Programming
shadcn/uiを使ってReactでの開発を加速させよう!
lef237
0
170
HTML/CSS超絶浅い説明
yuki0329
0
140
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
1.2k
KubeCon NA 2024の全DB関連セッションを紹介
nnaka2992
0
100
非ブラウザランタイムとWeb標準 / Non-Browser Runtimes and Web Standards
petamoriken
0
410
Fibonacci Function Gallery - Part 1
philipschwarz
PRO
0
260
開発者とQAの越境で自動テストが増える開発プロセスを実現する
92thunder
1
220
Внедряем бюджетирование, или Как сделать хорошо?
lamodatech
0
830
Amazon S3 NYJavaSIG 2024-12-12
sullis
0
130
命名をリントする
chiroruxx
1
530
menu基盤チームによるGoogle Cloudの活用事例~Application Integration, Cloud Tasks編~
yoshifumi_ishikura
0
130
毎日13時間もかかるバッチ処理をたった3日で60%短縮するためにやったこと
sho_ssk_
1
480
Featured
See All Featured
Reflections from 52 weeks, 52 projects
jeffersonlam
348
20k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
RailsConf 2023
tenderlove
29
960
No one is an island. Learnings from fostering a developers community.
thoeni
19
3.1k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
49k
Music & Morning Musume
bryan
46
6.3k
Speed Design
sergeychernyshev
25
710
YesSQL, Process and Tooling at Scale
rocio
170
14k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
7
540
Designing for Performance
lara
604
68k
Raft: Consensus for Rubyists
vanstee
137
6.7k
Transcript
@jenny_codes Fake your test away How to abuse your test
doubles
@jenny_codes
@jenny_codes @ RubyConf Taiwan 2019
@jenny_codes @ RubyConf Taiwan 2019
@jenny_codes @ RubyConf Taiwan 2019
@jenny_codes Let’s talk about Douglas Adams
@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
@jenny_codes Let’s talk about Douglas Adams Three stages in human
civilization: • Survival • Inquiry • Sophistication
@jenny_codes Let’s talk about Douglas Adams Three stages in human
civilization: • Survival: How? • Inquiry: Why? • Sophistication: Where?
@jenny_codes Let’s talk about Douglas Adams Three stages in human
civilization: • Survival: How can we eat? • Inquiry: Why? • Sophistication: Where?
@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?
@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?
@jenny_codes Let’s talk about testing
@jenny_codes Three stages in my relationship with test doubles: Let’s
talk about testing
@jenny_codes Three stages in my relationship with test doubles •
Survival: How to use test doubles? Let’s talk about testing
@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
@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
@jenny_codes Outline • Unit tests, integration tests, and those in
between • Integration-test wannabes • Helicopter unit tests • How to not abuse your test doubles?
@jenny_codes Outline • Unit tests, integration tests, and those in
between • Integration-test wannabes • Helicopter unit tests • How to not abuse your test doubles?
@jenny_codes Test double
@jenny_codes Test double ├─ Stub ├─ Mock └─ Spy
@jenny_codes Subject
@jenny_codes Dependency
Imagine a perfect world
@jenny_codes Testing pyramid Unit tests Integration tests Acceptance tests
@jenny_codes Testing pyramid, reality edition Integration-test wannabes Helicopter unit tests
Scream tests 🤔 🤔 🤔 🤔
@jenny_codes Testing pyramid, reality edition Helicopter unit tests Scream tests
🤔 🤔 🤔 🤔 Integration-test wannabes
@jenny_codes Testing pyramid, reality edition Helicopter unit tests Scream tests
🤔 🤔 🤔 🤔 Integration-test wannabes
@jenny_codes Testing pyramid, reality edition Helicopter unit tests Scream tests
🤔 🤔 🤔 🤔 Integration-test wannabes
@jenny_codes Outline • Unit tests, integration tests, and those in
between • Integration-test wannabes • Helicopter unit tests • How to not abuse your test doubles?
@jenny_codes A restaurant that serves French toast
@jenny_codes A restaurant that serves French toast
@jenny_codes A restaurant that serves French toast
@jenny_codes A restaurant that serves French toast
@jenny_codes A restaurant that serves French toast
@jenny_codes The test
@jenny_codes The test
@jenny_codes 🤔 The test
@jenny_codes
@jenny_codes 🔎
@jenny_codes 🔎
@jenny_codes 🔎 🚨
@jenny_codes
@jenny_codes
@jenny_codes
@jenny_codes
@jenny_codes
@jenny_codes
@jenny_codes
@jenny_codes
@jenny_codes ⛔
@jenny_codes
@jenny_codes
@jenny_codes
@jenny_codes Integration-test wannabes (n.) Tests with randomly inserted test doubles
because the real objects are too awkward to deal with.
@jenny_codes ❌ Random doubles == Random test failures Integration-test wannabes
@jenny_codes One month later…
@jenny_codes One month later… FAILED
@jenny_codes Therapeutic Refactoring by Katrina Owen, Cascadia Ruby Conf 2012
@jenny_codes
@jenny_codes when I don’t have to modify 100 random test
failures for it
@jenny_codes ❌ Random doubles == Random test failures ❌ Decreased
productivity Integration-test wannabes
Test doubles are contracts
@jenny_codes Outline • Unit tests, integration tests, and those in
between • Integration-test wannabes • Helicopter unit tests • How to not abuse your test doubles?
@jenny_codes Restaurant Service: Cheese Panini
@jenny_codes
@jenny_codes
@jenny_codes
@jenny_codes
@jenny_codes Chef Waiter Restaurant Service The dependency graph Floor 🛸
🌝 🐢 Cupboard Fridge Bread Cheese class-level dependency instance-level dependency
@jenny_codes The dependency graph 🙈 🙈 Chef Waiter Restaurant Service
Cupboard Fridge Bread Cheese class-level dependency instance-level dependency test double 🙈 🙈 🙈
@jenny_codes The dependency graph 🙈 🙈 Chef Waiter Restaurant Service
class-level dependency instance-level dependency test double 🙈 🙈 🙈 🙈 🙈
@jenny_codes
@jenny_codes
@jenny_codes
@jenny_codes
@jenny_codes
@jenny_codes
@jenny_codes
@jenny_codes
@jenny_codes Helicopter unit tests (n.) Tests that try to control
every implementation detail in the subject.
@jenny_codes ❌ Fake empire The helicopter unit tests
@jenny_codes https://media.giphy.com/media/IguL9zRYV20X1QeKuV/giphy.gif Production Your CI The helicopter unit tests
@jenny_codes ❌ Fake empire ❌ Brittle tests The helicopter unit
tests
@jenny_codes ❌ Fake empire ❌ Brittle tests The helicopter unit
tests GUARANTEED
@jenny_codes ❌ Fake empire ❌ Brittle tests The helicopter unit
tests x N
@jenny_codes Outline • Unit tests, integration tests, and those in
between • Integration-test wannabes • Helicopter unit tests • How to not abuse your test doubles?
@jenny_codes hard-to-use code == hard-to-write test easy-to-use code == easy-to-write
test
@jenny_codes Tests are the first consumer of your API
@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
@jenny_codes • 🎁 • 🎁 • 🎁 How to not
abuse your test doubles?
@jenny_codes • Use the Principle of Least Knowledge • 🎁
• 🎁 How to not abuse your test doubles?
@jenny_codes Principle of Least Knowledge Don’t talk to strangers
@jenny_codes Principle of Least Knowledge
@jenny_codes Principle of Least Knowledge Life Universe Everything Answer
@jenny_codes Principle of Least Knowledge Life Universe Everything Answer
@jenny_codes Principle of Least Knowledge Life Universe Answer Everything
@jenny_codes Principle of Least Knowledge Answer Everything Universe Life
@jenny_codes Principle of Least Knowledge Don’t talk to strangers Low
coupling
@jenny_codes Principle of Least Knowledge Don’t talk to strangers Low
coupling
@jenny_codes Principle of Least Knowledge Don’t talk to strangers Low
coupling
@jenny_codes
@jenny_codes
@jenny_codes
@jenny_codes
@jenny_codes
@jenny_codes
@jenny_codes Use Principle of Least Knowledge: Result
@jenny_codes Use Principle of Least Knowledge: Result
@jenny_codes • Use the Principle of Least Knowledge • 🎁
• 🎁 How to not abuse your test doubles?
@jenny_codes How to not abuse your test doubles? • Use
the Principle of Least Knowledge • Separate decisions and delegations. • 🎁
@jenny_codes Decision • Logic • Condition • Calculation • Dependency
• Relationships Delegation
@jenny_codes
@jenny_codes = delegation = decision
@jenny_codes 1. Get resources 2. Prepare ingredients 3. Make dish
4. Serve dish
@jenny_codes 1. Get resources
@jenny_codes 1. Get resources 2. Prepare ingredients 3. Make dish
4. Serve dish
@jenny_codes 1. Get resources 2. Prepare ingredients 3. Make dish
4. Serve dish => waiter => waiter => chef
@jenny_codes Separate decision and delegation: Result
@jenny_codes Separate decision and delegation: Result
@jenny_codes
@jenny_codes
@jenny_codes
@jenny_codes Separate decision and delegation: Result Arrange Act Assert
@jenny_codes Separate decision and delegation: Result
@jenny_codes Decision • Logic • Condition • Calculation • Unit
test • Dependency • Relationships Delegation
@jenny_codes Decision • Logic • Condition • Calculation • Unit
test • Dependency • Relationships • Integration test Delegation
@jenny_codes Testing pyramid with decision and delegation Unit tests Integration
tests Acceptance tests Decisions Delegation
@jenny_codes How to not abuse your test doubles? • Use
the Principle of Least Knowledge • Separate decisions and delegations • 🎁
@jenny_codes • Use the Principle of Least Knowledge • Separate
decision and delegation • Make the dependencies really obvious How to not abuse your test doubles?
@jenny_codes All dependencies are equal, but some are more equal
than others.
@jenny_codes All dependencies are equal, but some are more equal
than others. the side-e ff ect free the rest
@jenny_codes All dependencies are equal, but some are more equal
than others. the side-e ff ect free the rest 🤤
@jenny_codes All dependencies are equal, but some are more equal
than others. the side-e ff ect free the rest 🤤 ⚠
@jenny_codes the side-e ff ect free 🤤 All dependencies are
equal, but some are more equal than others. the rest ⚠ ⚠ ⚠ ⚠ ⚠ ⚠⚠⚠ ⚠
@jenny_codes Revisit serve_french_toast
@jenny_codes Chef Waiter Restaurant Service Floor 🛸 🌝 🐢 class-level
dependency instance-level dependency Revisit serve_french_toast
@jenny_codes Revisit serve_french_toast Chef Waiter Restaurant Service Floor 🛸 🌝
🐢 class-level dependency instance-level dependency
@jenny_codes Revisit serve_french_toast Chef Waiter Restaurant Service Floor 🛸 🌝
🐢 class-level dependency instance-level dependency
@jenny_codes Revisit serve_french_toast Chef Waiter Restaurant Service Floor 🛸 🌝
🐢 class-level dependency instance-level dependency
@jenny_codes Revisit serve_french_toast Chef Waiter Restaurant Service Floor 🛸 🌝
🐢 class-level dependency instance-level dependency How to make those dependencies obvious?
@jenny_codes Revisit serve_french_toast Chef Waiter Restaurant Service Floor 🛸 🌝
🐢 class-level dependency instance-level dependency
@jenny_codes Chef Waiter Restaurant Service Floor 🛸 🌝 🐢
@jenny_codes Chef Waiter Restaurant Service Floor 🛸 🌝 🐢
@jenny_codes Chef Waiter Restaurant Service Floor 🛸 🌝 🔻 🐢
Restaurant Repo
@jenny_codes Chef Waiter Restaurant Service Floor 🛸 🌝 🔻 🐢
Restaurant Repo
@jenny_codes
@jenny_codes ⚠ ⚠ ⚠
@jenny_codes
@jenny_codes
@jenny_codes Make dependencies obvious: Result
@jenny_codes Make dependencies obvious: Result
@jenny_codes Make dependencies obvious: Result
@jenny_codes • Use the Principle of Least Knowledge • Separate
decision and delegation • Make the dependencies really obvious How to not abuse your test doubles?
@jenny_codes Design your system so there’s no room to abuse
your test doubles
One last thing
@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
@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
@jenny_codes Talk to me! or just @jenny_codes