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
720
Context-Driven Development: Architecture your code with Phoenix context
jennyshih
0
880
Other Decks in Programming
See All in Programming
C++でシェーダを書く
fadis
6
4.1k
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
120
Remix on Hono on Cloudflare Workers
yusukebe
1
300
初めてDefinitelyTypedにPRを出した話
syumai
0
420
Compose 1.7のTextFieldはPOBox Plusで日本語変換できない
tomoya0x00
0
200
React への依存を最小にするフロントエンド設計
takonda
12
3.2k
見せてあげますよ、「本物のLaravel批判」ってやつを。
77web
7
7.8k
Jakarta EE meets AI
ivargrimstad
0
190
.NET のための通信フレームワーク MagicOnion 入門 / Introduction to MagicOnion
mayuki
1
1.8k
카카오페이는 어떻게 수천만 결제를 처리할까? 우아한 결제 분산락 노하우
kakao
PRO
0
110
Arm移行タイムアタック
qnighy
0
340
CSC509 Lecture 12
javiergs
PRO
0
160
Featured
See All Featured
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
0
110
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
Site-Speed That Sticks
csswizardry
0
33
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
27
4.3k
Ruby is Unlike a Banana
tanoku
97
11k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
It's Worth the Effort
3n
183
27k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
The Invisible Side of Design
smashingmag
298
50k
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