SlideShare une entreprise Scribd logo
1  sur  29
Dennis van der Stelt

Introduction in the TDD Mantra
Session Code: NE.17
Introduction
• Class-A
–
–
–
–

Kennisprovider
Microsoft development
Training / Coaching
www.class-a.nl

• Dennis van der Stelt
– Trainer / Coach
– Weblog at http://bloggingabout.net/blogs/dennis/
Agenda
•
•
•
•
•
•
•
•

A quick introduction into unit testing
Test Driven Development mantra
Refactoring
Benefits of Test Driven Development
Demo
Key rules of Test Driven Development
Mocking / Stubbing
Legacy code
A quick introduction
Unit testing
1. Automated testing
2. Facilitates change
3. Simplifies integration
4. Documentation
unit testing isn’t about just testing code
it‘s about allowing you to
determine when behavior changes
TDD Mantra
RED

GREEN
I’M TEST-DRIVEN

•
•
•
•

Write the test
Write the code
Refactor
Repeat

REFACTOR
TDD Mantra
Where to begin?
• Work from specification (or notes, or napkin)
• Create a list of tests
• Pick the simplest one
• Make test, make code
• Refactor

• Find a bug? Create the test that would’ve
caught it!
Test Driven Development
...is not about testing.
• It’s about design
• It’s about unambigious requirements
• It’s also about simplicity and YAGNI
Simplicity is more complicated then you think. But it’s well worth it.
Ron Jeffries, Extreme Programming Installed, 2001
Test Driven Development
...is about simple design that
1.
2.
3.
4.

Runs all tests
Reveals all intention
Has no duplication
Has the fewest number of classes and methods
Test Driven Development
“The act of writing a unit test is more an act of design then of verification”

- Robert C. Martin
“Test-driven development seeks specification, not validation, letting you think
through your design before you write your functional code”

- Scott Ambler
“Test-Driven Development is a powerful way to produce well designed code
with fewer defects”

- Martin Fowler
“Fewer defects, less debugging, more confidence, better design, and higher
productivity in my programming practice”

- Kent Beck
Benefits of TDD (1/3)
Benefits on your design & code
• Exposes design flaws & invalid assumptions
• Simpler class hierarchies
• Less code, smaller functions
• Less conditional code
• Faster development time

• Tests are both design & documentation
Benefits of TDD (2/3)
Benefits on your tests & bugs
• Tests cover *everything*
• Bugs found earlier & faster
• Bugs reproduced more easily
• Less (difficult) debugging
Benefits of TDD (3/3)
Cost of change

Test-Driven Development embraces change!
Waterfall
Cons of TDD
•
•
•
•

Needs practice, but you’ll learn to love it!
Needs discipline
Need to know OODP
Scares away managers
Bowling game
First a quick design session…
Bowling game
0 4
4

2 4
10

4 3
17

4 + (2+4)

•
•
•
•

6
30

3 2
35

55

9
65

0 0
65

9 0
74

7 2
83

17 + (6+4+3) 35 + (10+9+1) + 1 + 0)
55 + (9

10 frames, each holds 2 throws
Spare: 10 pins 2nd try, next throw added extra
Strike: 10 pins in 1st try, score next frame added
10th frame spare or strike,
player can make extra balls to complete frame.
Quick design sessions
next frame

Frame

Game
+roll(pins : int)
+score() : int

The score for spare or strike
depends on the frame’s succesor

10

+score() : int

Roll
1..2

-pins : int

1

We clearlyfunction must A game has ten frames. A frame has 1 or 2 roles.
The score need the Game class.
iterate
through all the frames, and
calculate all their scores.
tenth frame

The tenth frame has two or three roles.
It is different from all the other frames.
Bowling game
Finally some code!
Key rules of TDD
A test is no unit test if…
• …it talks to a database
• …it communicates across the network
• …it touches the file system
• …it can’t run at the same time as other tests
• …you have to perform actions to run the tests
(ex. configuration change)
How to isolate your code
How to isolate your code

database

logging
service

remote
server
How to isolate your code
Mocks & Stubs
A mock or stub is a stunt double for…
• …an object outside your unit
• …an external object (database)
• …a complex object(s)
• …a not yet developed object

Mocking & Stubbing are different things!
Isolation framework
[TestMethod]
public void Does_AllowDependencyInjection_When_CreatingInstance()
{
// Arrange
var fake = Isolate.Fake.Instance<ICustomerRepository>();
Customer cust = new Customer();
// Act
BusinessAction ba = new BusinessAction(fake);
ba.PerformSomeSortOfAction(cust);

// Assert
Isolate.Verify.
WasCalledWithExactArguments(() => fake.SaveChanges(cust));
}
Legacy code
What is legacy code?
Wikipedia : Legacy code is source code that relates
to a no-longer supported or manufactured
operating system or other computer system.

Why is legacy code bad?
• The previous developer isn’t there to explain it.
• It takes time to learn the code.
• It’s not easy to change the code.
• You might break some part of the application
you didn’t even touch.
Legacy code
Now imagine...
Without a tests,
• You’re developer on unitproject,
What if you have unit tests? as we speak!
you’re writing legacy code
• You get sick for two weeks,
• Yourhave tests to explain the code
You code needs to be completed,
• Another developer comes in.
You have tests to learn the code
© Michael Feathers – Working Effectively with Legacy Code

You have tests to verify what you did works,
and did not break any other part of the
application.

- The previous developer isn’t there to explain it.
- It takes time to learn the code.
- It’s not easy to change the code.
- You might break some part of the application you didn’t even touch.
Conclusion:

WRITE UNIT TESTS!
and do it the right way using Test-Driven Development.
Thank you for your attention
• Dennis van der Stelt
– Dennis@BloggingAbout.NET
– http://twitter.com/dvdstelt/
– http://bloggingabout.net/blogs/dennis/
Resources to more information can be found here, incl. this slidedeck.
Evaluation form
Vul je evaluatieformulier in en maak kans
op een van de prachtige prijzen!!
Fill out your evaluation form and win one of
the great prizes!!

Session Code: NE.17

Contenu connexe

Tendances

Testing and TDD - KoJUG
Testing and TDD - KoJUGTesting and TDD - KoJUG
Testing and TDD - KoJUG
lburdz
 
Test Driven Development (C#)
Test Driven Development (C#)Test Driven Development (C#)
Test Driven Development (C#)
Alan Dean
 
Test driven development
Test driven developmentTest driven development
Test driven development
Marcelo Cure
 

Tendances (20)

Testing and TDD - KoJUG
Testing and TDD - KoJUGTesting and TDD - KoJUG
Testing and TDD - KoJUG
 
2016 10-04: tdd++: tdd made easier
2016 10-04: tdd++: tdd made easier2016 10-04: tdd++: tdd made easier
2016 10-04: tdd++: tdd made easier
 
Exception Handling
Exception Handling  Exception Handling
Exception Handling
 
Java Testing With Spock - Ken Sipe (Trexin Consulting)
Java Testing With Spock - Ken Sipe (Trexin Consulting)Java Testing With Spock - Ken Sipe (Trexin Consulting)
Java Testing With Spock - Ken Sipe (Trexin Consulting)
 
Test Drive Development
Test Drive DevelopmentTest Drive Development
Test Drive Development
 
Clean Unit Test Patterns
Clean Unit Test PatternsClean Unit Test Patterns
Clean Unit Test Patterns
 
@LinkingNote annotation in YATSPEC
@LinkingNote annotation in YATSPEC@LinkingNote annotation in YATSPEC
@LinkingNote annotation in YATSPEC
 
A Brief Introduction to Test-Driven Development
A Brief Introduction to Test-Driven DevelopmentA Brief Introduction to Test-Driven Development
A Brief Introduction to Test-Driven Development
 
Exploratory testing workshop
Exploratory testing workshopExploratory testing workshop
Exploratory testing workshop
 
Test driven-development
Test driven-developmentTest driven-development
Test driven-development
 
Clean code - Getting your R&D on board
Clean code - Getting your R&D on boardClean code - Getting your R&D on board
Clean code - Getting your R&D on board
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In Action
 
Test Driven Development (C#)
Test Driven Development (C#)Test Driven Development (C#)
Test Driven Development (C#)
 
Grails unit testing
Grails unit testingGrails unit testing
Grails unit testing
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
TDD CrashCourse Part3: TDD Techniques
TDD CrashCourse Part3: TDD TechniquesTDD CrashCourse Part3: TDD Techniques
TDD CrashCourse Part3: TDD Techniques
 
Getting started with Test Driven Development
Getting started with Test Driven DevelopmentGetting started with Test Driven Development
Getting started with Test Driven Development
 
TDD: Develop, Refactor and Release With Confidence
TDD: Develop, Refactor and Release With ConfidenceTDD: Develop, Refactor and Release With Confidence
TDD: Develop, Refactor and Release With Confidence
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...
 

En vedette

En vedette (11)

Distributed Systems principles
Distributed Systems principlesDistributed Systems principles
Distributed Systems principles
 
LIDNUG : Reliable applications with messaging
LIDNUG : Reliable applications with messagingLIDNUG : Reliable applications with messaging
LIDNUG : Reliable applications with messaging
 
Building reliable applications with messaging
Building reliable applications with messagingBuilding reliable applications with messaging
Building reliable applications with messaging
 
Death of the batch job - NServiceBus Sagas
Death of the batch job - NServiceBus SagasDeath of the batch job - NServiceBus Sagas
Death of the batch job - NServiceBus Sagas
 
Een andere kijk op Microservices
Een andere kijk op MicroservicesEen andere kijk op Microservices
Een andere kijk op Microservices
 
Death of the batch job
Death of the batch jobDeath of the batch job
Death of the batch job
 
AppFabric Velocity
AppFabric VelocityAppFabric Velocity
AppFabric Velocity
 
Silverlight & WCF RIA
Silverlight & WCF RIASilverlight & WCF RIA
Silverlight & WCF RIA
 
Distributed Systems Design
Distributed Systems DesignDistributed Systems Design
Distributed Systems Design
 
Duplicating data or replicating data in Micro Services
Duplicating data or replicating data in Micro ServicesDuplicating data or replicating data in Micro Services
Duplicating data or replicating data in Micro Services
 
Distributed Systems Principles
Distributed Systems PrinciplesDistributed Systems Principles
Distributed Systems Principles
 

Similaire à Test Driven Development

Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
Codecamp Romania
 
Unit Testing Best Practices
Unit Testing Best PracticesUnit Testing Best Practices
Unit Testing Best Practices
Tomaš Maconko
 

Similaire à Test Driven Development (20)

TDD - Seriously, try it! - Trójmiasto Java User Group (17th May '23)
TDD - Seriously, try it! - Trójmiasto Java User Group (17th May '23)TDD - Seriously, try it! - Trójmiasto Java User Group (17th May '23)
TDD - Seriously, try it! - Trójmiasto Java User Group (17th May '23)
 
TDD - Seriously, try it! - Trjjmiasto JUG (17th May '23)
TDD - Seriously, try it! - Trjjmiasto JUG (17th May '23)TDD - Seriously, try it! - Trjjmiasto JUG (17th May '23)
TDD - Seriously, try it! - Trjjmiasto JUG (17th May '23)
 
TDD and Related Techniques for Non Developers (2012)
TDD and Related Techniques for Non Developers (2012)TDD and Related Techniques for Non Developers (2012)
TDD and Related Techniques for Non Developers (2012)
 
TDD - Seriously, try it! - Bucarest Tech Week
TDD - Seriously, try it! - Bucarest Tech WeekTDD - Seriously, try it! - Bucarest Tech Week
TDD - Seriously, try it! - Bucarest Tech Week
 
TDD - Seriously, try it! (updated '22)
TDD - Seriously, try it! (updated '22)TDD - Seriously, try it! (updated '22)
TDD - Seriously, try it! (updated '22)
 
TDD - Seriously, try it! - Opensouthcode
TDD - Seriously, try it! - OpensouthcodeTDD - Seriously, try it! - Opensouthcode
TDD - Seriously, try it! - Opensouthcode
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven Development
 
Joe Cisar - Everything I Know About TDD - Agile Midwest 2019
Joe Cisar - Everything I Know About TDD - Agile Midwest 2019Joe Cisar - Everything I Know About TDD - Agile Midwest 2019
Joe Cisar - Everything I Know About TDD - Agile Midwest 2019
 
TDD - Christchurch APN May 2012
TDD - Christchurch APN May 2012TDD - Christchurch APN May 2012
TDD - Christchurch APN May 2012
 
Test driven development v1.0
Test driven development v1.0Test driven development v1.0
Test driven development v1.0
 
TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019
 
Take Pride in Your Code - Test-Driven Development
Take Pride in Your Code - Test-Driven DevelopmentTake Pride in Your Code - Test-Driven Development
Take Pride in Your Code - Test-Driven Development
 
Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
 
Behaviour Driven Development and Thinking About Testing
Behaviour Driven Development and Thinking About TestingBehaviour Driven Development and Thinking About Testing
Behaviour Driven Development and Thinking About Testing
 
Unit Testing Best Practices
Unit Testing Best PracticesUnit Testing Best Practices
Unit Testing Best Practices
 
Adopting tdd in the workplace
Adopting tdd in the workplaceAdopting tdd in the workplace
Adopting tdd in the workplace
 
Adopting tdd in the workplace
Adopting tdd in the workplaceAdopting tdd in the workplace
Adopting tdd in the workplace
 
Testing Automaton - CFSummit 2016
Testing Automaton - CFSummit 2016Testing Automaton - CFSummit 2016
Testing Automaton - CFSummit 2016
 
Testing automaton
Testing automatonTesting automaton
Testing automaton
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)
 

Plus de Dennis van der Stelt

Plus de Dennis van der Stelt (7)

Change your architecture during deployment
Change your architecture during deploymentChange your architecture during deployment
Change your architecture during deployment
 
Dealing with eventual consistency
Dealing with eventual consistencyDealing with eventual consistency
Dealing with eventual consistency
 
Dealing with eventual consistency
Dealing with eventual consistencyDealing with eventual consistency
Dealing with eventual consistency
 
Continuous integration
Continuous integrationContinuous integration
Continuous integration
 
App fabric introduction
App fabric introductionApp fabric introduction
App fabric introduction
 
SOLID Principles part 2
SOLID Principles part 2SOLID Principles part 2
SOLID Principles part 2
 
SOLID Principles part 1
SOLID Principles part 1SOLID Principles part 1
SOLID Principles part 1
 

Dernier

Dernier (20)

What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
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
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
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...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
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
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
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...
 

Test Driven Development

  • 1. Dennis van der Stelt Introduction in the TDD Mantra Session Code: NE.17
  • 2. Introduction • Class-A – – – – Kennisprovider Microsoft development Training / Coaching www.class-a.nl • Dennis van der Stelt – Trainer / Coach – Weblog at http://bloggingabout.net/blogs/dennis/
  • 3. Agenda • • • • • • • • A quick introduction into unit testing Test Driven Development mantra Refactoring Benefits of Test Driven Development Demo Key rules of Test Driven Development Mocking / Stubbing Legacy code
  • 4. A quick introduction Unit testing 1. Automated testing 2. Facilitates change 3. Simplifies integration 4. Documentation
  • 5. unit testing isn’t about just testing code it‘s about allowing you to determine when behavior changes
  • 6. TDD Mantra RED GREEN I’M TEST-DRIVEN • • • • Write the test Write the code Refactor Repeat REFACTOR
  • 7. TDD Mantra Where to begin? • Work from specification (or notes, or napkin) • Create a list of tests • Pick the simplest one • Make test, make code • Refactor • Find a bug? Create the test that would’ve caught it!
  • 8. Test Driven Development ...is not about testing. • It’s about design • It’s about unambigious requirements • It’s also about simplicity and YAGNI Simplicity is more complicated then you think. But it’s well worth it. Ron Jeffries, Extreme Programming Installed, 2001
  • 9. Test Driven Development ...is about simple design that 1. 2. 3. 4. Runs all tests Reveals all intention Has no duplication Has the fewest number of classes and methods
  • 10. Test Driven Development “The act of writing a unit test is more an act of design then of verification” - Robert C. Martin “Test-driven development seeks specification, not validation, letting you think through your design before you write your functional code” - Scott Ambler “Test-Driven Development is a powerful way to produce well designed code with fewer defects” - Martin Fowler “Fewer defects, less debugging, more confidence, better design, and higher productivity in my programming practice” - Kent Beck
  • 11. Benefits of TDD (1/3) Benefits on your design & code • Exposes design flaws & invalid assumptions • Simpler class hierarchies • Less code, smaller functions • Less conditional code • Faster development time • Tests are both design & documentation
  • 12. Benefits of TDD (2/3) Benefits on your tests & bugs • Tests cover *everything* • Bugs found earlier & faster • Bugs reproduced more easily • Less (difficult) debugging
  • 13. Benefits of TDD (3/3) Cost of change Test-Driven Development embraces change! Waterfall
  • 14. Cons of TDD • • • • Needs practice, but you’ll learn to love it! Needs discipline Need to know OODP Scares away managers
  • 15. Bowling game First a quick design session…
  • 16. Bowling game 0 4 4 2 4 10 4 3 17 4 + (2+4) • • • • 6 30 3 2 35 55 9 65 0 0 65 9 0 74 7 2 83 17 + (6+4+3) 35 + (10+9+1) + 1 + 0) 55 + (9 10 frames, each holds 2 throws Spare: 10 pins 2nd try, next throw added extra Strike: 10 pins in 1st try, score next frame added 10th frame spare or strike, player can make extra balls to complete frame.
  • 17. Quick design sessions next frame Frame Game +roll(pins : int) +score() : int The score for spare or strike depends on the frame’s succesor 10 +score() : int Roll 1..2 -pins : int 1 We clearlyfunction must A game has ten frames. A frame has 1 or 2 roles. The score need the Game class. iterate through all the frames, and calculate all their scores. tenth frame The tenth frame has two or three roles. It is different from all the other frames.
  • 19. Key rules of TDD A test is no unit test if… • …it talks to a database • …it communicates across the network • …it touches the file system • …it can’t run at the same time as other tests • …you have to perform actions to run the tests (ex. configuration change)
  • 20. How to isolate your code
  • 21. How to isolate your code database logging service remote server
  • 22. How to isolate your code
  • 23. Mocks & Stubs A mock or stub is a stunt double for… • …an object outside your unit • …an external object (database) • …a complex object(s) • …a not yet developed object Mocking & Stubbing are different things!
  • 24. Isolation framework [TestMethod] public void Does_AllowDependencyInjection_When_CreatingInstance() { // Arrange var fake = Isolate.Fake.Instance<ICustomerRepository>(); Customer cust = new Customer(); // Act BusinessAction ba = new BusinessAction(fake); ba.PerformSomeSortOfAction(cust); // Assert Isolate.Verify. WasCalledWithExactArguments(() => fake.SaveChanges(cust)); }
  • 25. Legacy code What is legacy code? Wikipedia : Legacy code is source code that relates to a no-longer supported or manufactured operating system or other computer system. Why is legacy code bad? • The previous developer isn’t there to explain it. • It takes time to learn the code. • It’s not easy to change the code. • You might break some part of the application you didn’t even touch.
  • 26. Legacy code Now imagine... Without a tests, • You’re developer on unitproject, What if you have unit tests? as we speak! you’re writing legacy code • You get sick for two weeks, • Yourhave tests to explain the code You code needs to be completed, • Another developer comes in. You have tests to learn the code © Michael Feathers – Working Effectively with Legacy Code You have tests to verify what you did works, and did not break any other part of the application. - The previous developer isn’t there to explain it. - It takes time to learn the code. - It’s not easy to change the code. - You might break some part of the application you didn’t even touch.
  • 27. Conclusion: WRITE UNIT TESTS! and do it the right way using Test-Driven Development.
  • 28. Thank you for your attention • Dennis van der Stelt – Dennis@BloggingAbout.NET – http://twitter.com/dvdstelt/ – http://bloggingabout.net/blogs/dennis/ Resources to more information can be found here, incl. this slidedeck.
  • 29. Evaluation form Vul je evaluatieformulier in en maak kans op een van de prachtige prijzen!! Fill out your evaluation form and win one of the great prizes!! Session Code: NE.17