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
190
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
chibiccをCILに移植した結果 (NGK2025S版)
kekyo
PRO
0
150
GitHub CopilotでTypeScriptの コード生成するワザップ
starfish719
26
6k
各クラウドサービスにおける.NETの対応と見解
ymd65536
0
250
どうして手を動かすよりもチーム内のコードレビューを優先するべきなのか
okashoi
3
900
見えないメモリを観測する: PHP 8.4 `pg_result_memory_size()` とSQL結果のメモリ管理
kentaroutakeda
0
950
[JAWS-UG横浜 #80] うわっ…今年のServerless アップデート、少なすぎ…?
maroon1st
0
120
いりゃあせ、PHPカンファレンス名古屋2025 / Welcome to PHP Conference Nagoya 2025
ttskch
1
200
Fixstars高速化コンテスト2024準優勝解法
eijirou
0
190
テストコード書いてみませんか?
onopon
2
350
ASP.NET Core の OpenAPIサポート
h455h1
0
130
Package Traits
ikesyo
1
210
盆栽転じて家具となる / Bonsai and Furnitures
aereal
0
1.9k
Featured
See All Featured
Unsuck your backbone
ammeep
669
57k
Done Done
chrislema
182
16k
We Have a Design System, Now What?
morganepeng
51
7.3k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Side Projects
sachag
452
42k
Practical Orchestrator
shlominoach
186
10k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
98
18k
Writing Fast Ruby
sferik
628
61k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
49k
VelocityConf: Rendering Performance Case Studies
addyosmani
327
24k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.2k
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