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
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
320
Testing value objects
franiglesias
0
280
testing the unpredictable
franiglesias
0
250
Tests doubles: the motion picture
franiglesias
0
540
Other Decks in Programming
See All in Programming
分散システム、なんですぐ死んでしまうん?耐障害性を高めたいあなたのためのレジリエンスパターン入門
mshibuya
7
5.4k
例外の正しい扱い方 そのエラー try-catchして大丈夫?
jinwatanabe
0
350
エンジニアと一緒にテストコードの設計と実装を改善した話
mototakatsu
0
260
霧の中の代数的エフェクト
funnyycat
1
320
Go1.27で導入されるジェネリクスメソッドでできること
mackee
0
260
Hatena Engineer Seminar #37「言語モデルの活用に関する研究」
slashnephy
0
500
AIエージェントで 変わるAndroid開発環境
takahirom
2
480
過去最大のMCPアップデート! 2026-07-28 RC版の謎に迫る
licux
6
460
Performance Engineering for Everyone
elenatanasoiu
0
260
鹿野さんに聞く!『TypeScriptコードレシピ集』で磨く実践力
tonkotsuboy_com
4
1k
関数型プログラミングのメリットって何だろう?
wanko_it
0
160
「なぜそう決めたのか」を残し続ける仕組み ― Notion AI カスタムエージェント × Slack連携による設計判断の自動記録 - NIKKEI Tech Talk #47
niftycorp
PRO
0
260
Featured
See All Featured
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.3k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
630
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.8k
Mobile First: as difficult as doing things right
swwweet
225
10k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
500
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
290
4 Signs Your Business is Dying
shpigford
187
22k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.3k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.8k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
260
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