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 doubles
Search
Jenny Shih
November 11, 2021
Programming
0
140
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
570
Context-Driven Development: Architecture your code with Phoenix context
jennyshih
0
810
Other Decks in Programming
See All in Programming
[技育CAMPアカデミア]アイディアを形に!【超入門】スマホアプリ開発〜リリースまでの流れをご紹介
teamlab
PRO
0
360
Ruby Pattern Matching
bkuhlmann
0
920
新宿ダンジョンを可視化してみた
satoshi7190
2
240
データアナリストが行うDatabricksを活用したETLの自動化事例
shinoa
0
260
Ruby Function Composition
bkuhlmann
1
330
코틀린으로 멀티플랫폼 만들기
pangmoo
0
150
はてなにおける CSS Modules、及び CSS Modules に足りないもの / CSS Modules in Hatena, and CSS Modules missing parts
mizdra
7
920
VSCodeでのDatabricks開発もお勧めしたい/I would also recommend Databricks development with VSCode.
kazumain
0
250
効率化に挑戦してみたらモバイル開発が少し快適になった話
ryunakayama
0
130
try!Swift Tokyo 2024 参加報告 LT
akidon0000
1
220
Rethinking UI building strategies @ SFI 2024
letelete
0
270
AWS CDKコントリビュートTIPS / aws-cdk-contribution-tips
gotok365
2
120
Featured
See All Featured
KATA
mclloyd
15
12k
Ruby is Unlike a Banana
tanoku
96
10k
Visualization
eitanlees
136
14k
Robots, Beer and Maslow
schacon
PRO
155
7.9k
Reflections from 52 weeks, 52 projects
jeffersonlam
345
19k
Making the Leap to Tech Lead
cromwellryan
124
8.5k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
322
20k
Fontdeck: Realign not Redesign
paulrobertlloyd
76
4.9k
Building Applications with DynamoDB
mza
88
5.6k
Build The Right Thing And Hit Your Dates
maggiecrowley
24
2k
Optimising Largest Contentful Paint
csswizardry
8
2.4k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
155
14k
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