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
Low cost techniques for test doubles
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Fran Iglesias
November 02, 2018
Programming
240
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Low cost techniques for test doubles
Fran Iglesias
November 02, 2018
More Decks by Fran Iglesias
See All by Fran Iglesias
Tips for daily refactoring
franiglesias
0
360
Introduction to TDD: Red-Green-Refactor
franiglesias
1
330
Testing value objects
franiglesias
0
290
testing the unpredictable
franiglesias
0
260
Tests doubles: the motion picture
franiglesias
0
550
Other Decks in Programming
See All in Programming
SREの積み重ねがAI駆動開発のガードレールになった ― 7つの実践/SRE Guardrails The 7
tomoyakitaura
9
6k
AI時代のPHPer生存戦略 ~「言語、もうなんでもよくない?」に本気で向き合う~
vivion
0
220
生成AIで帳票OCRが「簡単に」作れる時代になった?
kon_shou
0
310
AIエージェントで 変わるAndroid開発環境
takahirom
2
760
生成AI導入の「期待外れ」を乗り越える ー 開発フロー改革が目指す、真の組織変革
starfish719
0
3.6k
ソフトウェア設計に溶けるインフラ ― AWS CDK のインフラ認識論
konokenj
3
740
AI時代に設計が 最大の生産性レバーになる 意図駆動開発とデータを消さない設計|Don't Delete Your Data or Your Intent — Design as the Deepest Lever in the AI Era
tomohisa
1
510
GDG Korea Android: 2026 I/O Extended ~ What's new in Android development tools
pluu
0
210
全PRの83%がAIレビューだけでマージできるようになった開発組織はその後どうなったか
athug
1
1.3k
ITヒヤリハットを整理してみた ~ライフサイクルと原因から考える再発防止策~
koukimiura
1
130
AI時代、エンジニアはどう育つのか -未経験エンジニアの成長を間近で見て考えたこと-
thasu0123
0
210
人間の目はかわらない、だからJPEGは30年もつ
yuzneri
12
17k
Featured
See All Featured
What's in a price? How to price your products and services
michaelherold
247
13k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
67
56k
Product Roadmaps are Hard
iamctodd
55
12k
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
230
Build your cross-platform service in a week with App Engine
jlugia
234
19k
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
1
380
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
My Coaching Mixtape
mlcsv
0
190
KATA
mclloyd
PRO
35
15k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
950
The SEO Collaboration Effect
kristinabergwall1
1
510
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
790
Transcript
TDD 101 5. Low cost techniques for doubles
Low cost techniques for doubles
The point of going low-cost Test should be • Light
• Fast • Easy to read • Easy to maintain
The point of going low-cost Reduce dependency on mocking frameworks
Strictly speaking, mocking frameworks break isolation principle Use the things you have at hand
Low cost techniques
Techniques at a glance Don’t double (if you don’t really
need it) Self shunt Anonymous class
Don’t double Use real objects when: • They have no
behavior or it is very simple • They have no side effects • They are immutable • They have no dependencies (or depend on similar objects)
Don’t double $request = new GetObjectsRequest (‘param1’, ‘param2’); $serviceUnderTest =
new Service(); $result = $serviceUnderTest->execute($request); $this->assertEquals(‘expected’, $result);
Self-shunt Use the TestCase as double (WTF!) Great for simple
doubles Great for early stages of TDD
Self-shunt You will need an interface to double Make the
test double implement interface You can implement spies
Self-shunt stub Class ServiceTest extends TestCase implements GetObjectsRequestInterface { public
function testSomething() { $serviceUnderTest = new Service(); $result = $serviceUnderTest->execute($this); $this->assertEquals(‘expected’, $result); } public function param1() { return ‘param1’; } public function param2() { return ‘param2’; } }
Self-shunt spy interface CollaboratorInterface { public function doThatThing(): string; }
We will need a collaborator for our service
Self-shunt spy class ServiceTest extends TestCase implements CollaboratorInterface {
private $collaboratorCalls = 0; public function testSomething() { $serviceUnderTest = new Service($this); $result = $serviceUnderTest->execute(); $this->assertEquals(‘expected’, $result); $this->assertEquals(1, $this->collaboratorCalls); } public function doThatThing(): string { $this->collaboratorCalls+; return ‘something’; } }
Anonymous class Create doubles on the fly Code exactly what
you need Test Abstract classes Doesn’t require a framework
Anonymous class Implement interface or subclass May use builders
Anonymous class class ServiceTest extends TestCase { public function testSomething()
{ $collaborator = new class () implements CollaboratorInterface { private $calls = 0; public function doThatThing(): string { $this->calls++; return ‘something’; } public function calls() { return $this->calls; } }; $serviceUnderTest = new Service($collaborator); $result = $serviceUnderTest->execute(); $this->assertEquals(‘expected’, $result); $this->assertEquals(1, $collaborator->calls()); } }
None