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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Fran Iglesias
November 02, 2018
Programming
250
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
370
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
560
Other Decks in Programming
See All in Programming
運用ダッシュボードの設計を誰も教えてくれないのだけどみなさんどうしてるんですか? - チームに監視するという文化を根付かせるための第一歩を踏みたい -
satoshi256kbyte
1
140
React本体のコードリーディング
high_g_engineer
1
150
まずはプロンプトガイドを読もう、話はそれからだ
kiakiraki
1
200
Building a Meta Ray-Ban display app
akkeylab
0
180
How I Won Prize Money at a Hackathon Using Codex and Symphony Alpha
yasei_no_otoko
0
120
Claude CodeとAgentCore Gatewayを繋ぐ際の認証認可 / Authentication and authorization when connecting Claude Code with AgentCore Gateway
har1101
2
360
複数の Claude Code が"放置"されてしまう問題をCLI ダッシュボードを自作して解決した話
sumihiro3
1
720
プロポーザルを書いてもらう
pvcresin
0
570
「つくるAI」だけではバグは見つからない ~テストに必要な「見つけるAI」を分離させる戦略~
mfunaki
0
160
AI Engineeringは、AIプロダクトだけのものか? 〜AIがソフトウェアを作る時代の新しい当たり前〜 / No AI in your product. AI Engineering in your development.
rkaga
5
510
VibeCodingからAgenticWorkflowへ
starfish719
0
790
Claude Code全社展開のためにやったことn選~プラグイン302個・コミッター271人を支えるために~
kenchan
5
1.7k
Featured
See All Featured
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.7k
My Coaching Mixtape
mlcsv
0
270
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
250
The untapped power of vector embeddings
frankvandijk
2
1.8k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
A Modern Web Designer's Workflow
chriscoyier
698
190k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
500
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
10k
Site-Speed That Sticks
csswizardry
13
1.5k
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
480
The agentic SEO stack - context over prompts
schlessera
0
870
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.2k
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