SlideShare ist ein Scribd-Unternehmen logo
1 von 46
Downloaden Sie, um offline zu lesen
Clean Code
in
Unit
Testing Mavi Jiménez
https://github.com/linkita/fee-calculator-tested
How is a unit
test
Asserts4
Mocks
Call
1
New2
3
public function testValidCalculateFeeRequestReturnFeeAsMoney()
{
$feeCalculatorMock = new FeeCalculatorMocks($this);
$tariffRepositoryMock = new TariffRepositoryMocks($this);
$calculateFeeRequestMock = new CalculateFeeRequestMocks($this);
$calculateFee = new CalculateFee(
$feeCalculatorMock->getMoney42(),
$tariffRepositoryMock->getTariffOne()
);
$money = $calculateFee->execute($calculateFeeRequestMock->complete());
$this->assertEquals($money->amount(), 42);
$this->assertEquals($money->currency(), Money::EUR);
}
public function testValidCalculateFeeRequestReturnFeeAsMoney()
{
$feeCalculatorMock = new FeeCalculatorMocks($this);
$tariffRepositoryMock = new TariffRepositoryMocks($this);
$calculateFeeRequestMock = new CalculateFeeRequestMocks($this);
$calculateFee = new CalculateFee(
$feeCalculatorMock->getMoney42(),
$tariffRepositoryMock->getTariffOne()
);
$money = $calculateFee->execute($calculateFeeRequestMock->complete());
$this->assertEquals($money->amount(), 42);
$this->assertEquals($money->currency(), Money::EUR);
}
public function testValidCalculateFeeRequestReturnFeeAsMoney()
{
$feeCalculatorMock = new FeeCalculatorMocks($this);
$tariffRepositoryMock = new TariffRepositoryMocks($this);
$calculateFeeRequestMock = new CalculateFeeRequestMocks($this);
$calculateFee = new CalculateFee(
$feeCalculatorMock->getMoney42(),
$tariffRepositoryMock->getTariffOne()
);
$money = $calculateFee->execute($calculateFeeRequestMock->complete());
$this->assertEquals($money->amount(), 42);
$this->assertEquals($money->currency(), Money::EUR);
}
public function testValidCalculateFeeRequestReturnFeeAsMoney()
{
$feeCalculatorMock = new FeeCalculatorMocks($this);
$tariffRepositoryMock = new TariffRepositoryMocks($this);
$calculateFeeRequestMock = new CalculateFeeRequestMocks($this);
$calculateFee = new CalculateFee(
$feeCalculatorMock->getMoney42(),
$tariffRepositoryMock->getTariffOne()
);
$money = $calculateFee->execute($calculateFeeRequestMock->complete());
$this->assertEquals($money->amount(), 42);
$this->assertEquals($money->currency(), Money::EUR);
}
assertTrue(true)
Let´s test, let´s mock
public function fourtyTwoEur()
{
$mock = $this->basicMock();
$mock
->expects($this->any())
->method('getAmount')
->willReturn(42);
$mock
->expects($this->any())
->method('getCurrency')
->willReturn(Money::EUR);
return $mock;
}
class Money
{
public const EUR = '€';
/** @var float */
private $amount;
/** @var string */
private $currency;
public function __construct(
float $amount,
string $currency
){
$this->amount = $amount;
$this->currency = $currency;
}
public function getAmount(): float
{
return $this->amount;
}
public function getCurrency(): string
{
return $this->currency;
}
}
Little garbage
/**
@var TestCase
*/
private $testCase;
public function __construct(TestCase $testCase)
{
$this->testCase = $testCase;
}
MockBuilder
class CalculateFeeTest extends TestCase
{
$calculateFeeRequestMock =
new CalculateFeeRequestMocks($this);
MockBuilder
/** @var ObjectProphecy */
private $prophet;
public function __construct(ObjectProphecy $prophet)
{
$this->prophet = $prophet;
}
Prophecy
$priceMock = new PriceMocks($this->prophesize(Price::class));
Prophecy
Which? How? Arg!
Testing!
Should be expressive
testMoneyFail
testOneWrongValueShouldThrowException
Should be expressive
testMoneyFail
:)
Happy path
Murphy's law
Corner cases
Less is more
Data providers
Make SOLID Tests
SOLID Single responsability
Open Close principle
Liskov substitution principle
Interface segregation principle
Dependency inversion
SOLID Single responsability
Open Close principle
Liskov substitution principle
Interface segregation principle
Dependency inversion
Make unit testing
SOLID Single responsability
Open Close principle
Liskov substitution principle
Interface segregation principle
Dependency inversion
New feature, new test class
SOLID Single responsability
Open Close principle
Liskov substitution principle
Interface segregation principle
Dependency inversion
All objects should be replaceable with
Mocks
SOLID Single responsability
Open Close principle
Liskov substitution principle
Interface segregation principle
Dependency inversion
Small interfaces, easy mocks
SOLID Single responsability
Open Close principle
Liskov substitution principle
Interface segregation principle
Dependency inversion
If you can't mock it, it's an antipattern!
Make
a
standard
documentation
testSemanticNameGivenReturnGoodKarma()
{
// ...
}
testClassFail()
{
// ...
}
/**
* @test
*/
shouldbeAtestWihoutAnnotation() {
// ...
}
bad
good
Clean Code
in
Unit
Testing
Clean Testing
Clean code in unit testing
Clean code in unit testing
Clean code in unit testing

Weitere ähnliche Inhalte

Was ist angesagt?

Operating Systems lab Programs - Fourth Semester - Engineering
Operating Systems lab Programs - Fourth Semester - EngineeringOperating Systems lab Programs - Fourth Semester - Engineering
Operating Systems lab Programs - Fourth Semester - Engineering
Yogesh Santhan
 
اسلاید دوم جلسه پنجم کلاس پایتون برای هکرهای قانونی
اسلاید دوم جلسه پنجم کلاس پایتون برای هکرهای قانونیاسلاید دوم جلسه پنجم کلاس پایتون برای هکرهای قانونی
اسلاید دوم جلسه پنجم کلاس پایتون برای هکرهای قانونی
Mohammad Reza Kamalifard
 
VLSI Sequential Circuits II
VLSI Sequential Circuits IIVLSI Sequential Circuits II
VLSI Sequential Circuits II
Gouthaman V
 

Was ist angesagt? (20)

C++ programs
C++ programsC++ programs
C++ programs
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C Language
 
C lab programs
C lab programsC lab programs
C lab programs
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shorting
 
OpenWRT Makefile reference
OpenWRT Makefile referenceOpenWRT Makefile reference
OpenWRT Makefile reference
 
C file
C fileC file
C file
 
Operator C# - Lec3 (Workshop on C# Programming: Learn to Build)
Operator  C# - Lec3 (Workshop on C# Programming: Learn to Build)Operator  C# - Lec3 (Workshop on C# Programming: Learn to Build)
Operator C# - Lec3 (Workshop on C# Programming: Learn to Build)
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
 
Simple C programs
Simple C programsSimple C programs
Simple C programs
 
programs
programsprograms
programs
 
Computer programing w
Computer programing wComputer programing w
Computer programing w
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
 
Operating Systems lab Programs - Fourth Semester - Engineering
Operating Systems lab Programs - Fourth Semester - EngineeringOperating Systems lab Programs - Fourth Semester - Engineering
Operating Systems lab Programs - Fourth Semester - Engineering
 
اسلاید دوم جلسه پنجم کلاس پایتون برای هکرهای قانونی
اسلاید دوم جلسه پنجم کلاس پایتون برای هکرهای قانونیاسلاید دوم جلسه پنجم کلاس پایتون برای هکرهای قانونی
اسلاید دوم جلسه پنجم کلاس پایتون برای هکرهای قانونی
 
C program to implement linked list using array abstract data type
C program to implement linked list using array abstract data typeC program to implement linked list using array abstract data type
C program to implement linked list using array abstract data type
 
some basic C programs with outputs
some basic C programs with outputssome basic C programs with outputs
some basic C programs with outputs
 
Double linked list
Double linked listDouble linked list
Double linked list
 
VLSI Sequential Circuits II
VLSI Sequential Circuits IIVLSI Sequential Circuits II
VLSI Sequential Circuits II
 

Ähnlich wie Clean code in unit testing

Ähnlich wie Clean code in unit testing (20)

Practical approach for testing your software with php unit
Practical approach for testing your software with php unitPractical approach for testing your software with php unit
Practical approach for testing your software with php unit
 
Symfony (Unit, Functional) Testing.
Symfony (Unit, Functional) Testing.Symfony (Unit, Functional) Testing.
Symfony (Unit, Functional) Testing.
 
Dutch php a short tale about state machine
Dutch php   a short tale about state machineDutch php   a short tale about state machine
Dutch php a short tale about state machine
 
PHPUnit elevato alla Symfony2
PHPUnit elevato alla Symfony2PHPUnit elevato alla Symfony2
PHPUnit elevato alla Symfony2
 
PHPunit and you
PHPunit and youPHPunit and you
PHPunit and you
 
Concurrent test frameworks
Concurrent test frameworksConcurrent test frameworks
Concurrent test frameworks
 
Advanced php testing in action
Advanced php testing in actionAdvanced php testing in action
Advanced php testing in action
 
My Development Story
My Development StoryMy Development Story
My Development Story
 
Load Testing with PHP and RedLine13
Load Testing with PHP and RedLine13Load Testing with PHP and RedLine13
Load Testing with PHP and RedLine13
 
Design how your objects talk through mocking
Design how your objects talk through mockingDesign how your objects talk through mocking
Design how your objects talk through mocking
 
Mocking Demystified
Mocking DemystifiedMocking Demystified
Mocking Demystified
 
PHPSpec BDD for PHP
PHPSpec BDD for PHPPHPSpec BDD for PHP
PHPSpec BDD for PHP
 
PHPUnit testing to Zend_Test
PHPUnit testing to Zend_TestPHPUnit testing to Zend_Test
PHPUnit testing to Zend_Test
 
TestFest - Respect\Validation 1.0
TestFest - Respect\Validation 1.0TestFest - Respect\Validation 1.0
TestFest - Respect\Validation 1.0
 
PHPSpec BDD Framework
PHPSpec BDD FrameworkPHPSpec BDD Framework
PHPSpec BDD Framework
 
Introduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitIntroduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnit
 
The command dispatcher pattern
The command dispatcher patternThe command dispatcher pattern
The command dispatcher pattern
 
Functional php
Functional phpFunctional php
Functional php
 
Introduction to Zend Framework web services
Introduction to Zend Framework web servicesIntroduction to Zend Framework web services
Introduction to Zend Framework web services
 
Modernising Legacy Code
Modernising Legacy CodeModernising Legacy Code
Modernising Legacy Code
 

Kürzlich hochgeladen

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Kürzlich hochgeladen (20)

Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 

Clean code in unit testing