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
350
Testing value objects
franiglesias
0
310
testing the unpredictable
franiglesias
0
280
Tests doubles: the motion picture
franiglesias
0
580
Other Decks in Programming
See All in Programming
Domain-Driven Transformation
hschwentner
2
2.4k
コードで差別化できない時代の ソフトウェアエンジニアリング / Software Engineering in an Era Where Code Doesn’t Differentiate
kishida
2
220
私なりのハーネスエンジニアリング ~独自の言語処理系の開発を通して~
seosoft
0
140
AI時代のFlutter品質設計 — アプリ品質を守るQuality Layers
omatty198
0
300
Inertia Rails: The One-Person Framework with React & Friends
skryukov
0
190
[2026-10-09]一枚の布で作らなくてもいい。 信頼性は「パッチワーク」で紡がれる~既存の運用と新しい可能性をつなぎ合わせてきたCREの話~
tosite
0
120
Lambda durable functionsでHuman in the Loopを楽に…楽に……
har1101
2
220
最近”作らせてる” ローカルAIアプリの話
akit37
0
510
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
490
No More NoSuchMethodError
exoego
1
120
個人開発者よ、Honoを使え(おじさんver)
tanahiro2010
2
290
R8 の設定見直しでアプリサイズ削減
andpad
0
230
Featured
See All Featured
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Ten Tips & Tricks for a 🌱 transition
stuffmc
1
270
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
340
[Rails World 2026] Durable orchestration on Rails: from continuation to workflow
palkan
2
960
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.7k
HDC tutorial
michielstock
2
980
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
3
3.9k
Thoughts on Productivity
jonyablonski
76
5.4k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
1
470
4 Signs Your Business is Dying
shpigford
187
23k
Designing for Performance
lara
612
70k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.6k
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