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
170
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
690
Context-Driven Development: Architecture your code with Phoenix context
jennyshih
0
870
Other Decks in Programming
See All in Programming
学生の時に開催したPerl入学式をきっかけにエンジニアが組織に馴染むために勉強会を主催や仲間と参加して職能間の境界を越えていく
ohmori_yusuke
1
130
RDBの世界をぬりかえていくモデルグラフDB〜truncus graphによるモデルファースト開発〜
jurabi
0
170
Rails 8 Frontend: 10 commandments & 7 deadly sins in 2025
yshmarov
1
630
perl for shell, awk and sed programmers
mackee
1
680
(Deep|Web) Link support with expo-router
mrtry
0
170
"Swarming" をコンセプトに掲げるアジャイルチームのベストプラクティス
boykush
2
250
Re:PandasAI:生成AIがデータ分析業務にもたらすパラダイムシフト【増補改訂版】
negi111111
1
910
色んなオートローダーを覗き見る #phpcon_okinawa
o0h
PRO
5
390
文化が生産性を作る
jimpei
3
560
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
9
990
CSC509 Lecture 04
javiergs
PRO
0
140
Beyond Laravel Octane - Hyperf for Laravel Artisans
albertcht
1
130
Featured
See All Featured
Practical Orchestrator
shlominoach
186
10k
Speed Design
sergeychernyshev
22
480
RailsConf 2023
tenderlove
28
840
Into the Great Unknown - MozCon
thekraken
31
1.4k
Embracing the Ebb and Flow
colly
84
4.4k
What's in a price? How to price your products and services
michaelherold
243
11k
YesSQL, Process and Tooling at Scale
rocio
167
14k
BBQ
matthewcrist
85
9.2k
In The Pink: A Labor of Love
frogandcode
139
22k
Documentation Writing (for coders)
carmenintech
65
4.3k
Building Better People: How to give real-time feedback that sticks.
wjessup
362
19k
Design by the Numbers
sachag
278
19k
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