Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Low cost techniques for test doubles
Search
Fran Iglesias
November 02, 2018
Programming
260
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
380
Introduction to TDD: Red-Green-Refactor
franiglesias
1
340
Testing value objects
franiglesias
0
300
testing the unpredictable
franiglesias
0
270
Tests doubles: the motion picture
franiglesias
0
570
Other Decks in Programming
See All in Programming
AHC070解法紹介
eijirou
0
150
Agentic Software Factoryに、すごく賢いIF文を。 / super smart IF statement into the Agentic Software Factory.
rkaga
5
1.4k
巨大モノリシックアプリ モダン化大作戦
ktcryomm
1
1.3k
テストを司るデーモンに会いに行く 〜隔離した仮想マシンでテストを通すまで〜
h1d3mun3
1
600
wkhtmltopdfの次どうするか問題2026
willnet
2
1.8k
[Rails World 2026] Durable orchestration on Rails: from continuation to workflow
palkan
1
430
ソニーのクラウド共通基盤の変遷とAI時代の開発スタイルに合わせた進化 / The Journey of Sony’s Common Cloud Platform and Its Evolution for AI-Native Development
kenjiyoneyama
0
220
GitHubハンズオン講座 — 実務レベルのチーム開発のフローを身につけよう
junhat6
0
120
仕様駆動開発による爆速プロダクト開発 / Bakusoku Spec Driven Development
kobakei
0
170
iOS 27でニュースアプリはどう変わる!? 〜日経電子版の新機能対応と、開発事例から〜
lynnswap
7
13k
Vue Fes Japan 2026 タイムテーブル徹底解説
448jp
1
600
Java 27新機能 / Java 27 new features
kishida
2
190
Featured
See All Featured
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.6k
Test your architecture with Archunit
thirion
2
2.4k
The Limits of Empathy - UXLibs8
cassininazir
1
700
Marketing to machines
jonoalderson
1
5.8k
Building the Perfect Custom Keyboard
takai
2
890
ラッコキーワード サービス紹介資料
rakko
1
5.1M
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.5k
How GitHub (no longer) Works
holman
316
150k
KATA
mclloyd
PRO
35
16k
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
290
The World Runs on Bad Software
bkeepers
PRO
72
12k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.9k
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