controlling dependencies 16-9 - quality coding• the art of unit testing by roy osherove resources...
TRANSCRIPT
Jon Reid
Test Driven Development
…by Controlling Dependencies
http://www.flickr.com/photos/stefan-w/3337072853
Write a failing test
Make the test pass
Refactor
UI?Networking?
UI!Networking!
http://www.flickr.com/photos/jsmoorman/2298671281
3 Types ofUnit Tests:
Return Value Test
State Test
Interaction Test
Return Value Test
Arrange: Set up object
Act: Call method that returns a value
Assert: Compare against expected value
call method
Test SUTvalueSystemUnderTest
State Test
Arrange: Set up object
Act: Call method
Assert: Query object in some way
Test SUT
call method
result
query
Interaction Test
Test SUT SomethingElse!
? ?
Interaction Test
Table
GetDishes
Waiter Cook
ProcessOrder
Interaction Test
Table
GetDishes
Waiter Cook
ProcessOrderFakeCook
DependencyInjection
Table
GetDishes
Waiter Cook
ProcessOrderFakeCook
Dependency Injection
• Extract and Override
• Method Injection
• Property Injection
• Constructor Injection
http://www.flickr.com/photos/8499561@N02/2755481069
Seams
http://www.flickr.com/photos/gileslane/2262575575
Extract and Override
Extract and Override
Method Injection
Method Injection
Property Injection
Property Injection
Property Injection
Constructor Injection
Dependency Injection
• Extract and Override
• Method Injection
• Property Injection
• Constructor Injection
• Ambient Context
http://www.flickr.com/photos/8499561@N02/2755481069
Dependency Injection
M A N N I N G
Mark SeemannFOREWORD BY GLENN BLOCK
in .NET
Interaction Test
SUTRealThing
SUT FakeThing
Production
Testing?
Types of Fakes
M A N N I N G
the art of
with examples in C#
FOREWORDS BY
Michael FeathersRobert C. Martin
SECOND EDITION
ROY OSHEROVE
Stub
Test
SUT Stubcommunicate
assert
(Fake)
Mock
Test
SUT Mockcommunicate
assert
(Fake)
“That’s great, Jon. …But how do I use this for networking?”
Networking Call
Inject dependency
Networking Call
Record number of calls
Networking Call
Fake return value
Networking Call
Capture arguments
Let’s Make a Fake:1. Stub the method
Let’s Make a Fake:2. Record number of calls
Let’s Make a Fake:3. Fake return value
Let’s Make a Fake:4. Capture arguments
Let’s Write a Test (1):Does it call GET exactly once?
1. Arrange
2. Act
3. Assert
Let’s Write a Test (2):Simulate a JSON Response!
1. Arrange
2. Act
3. Assert
Refactor Test Code:Spot Duplication
Refactor Test Code:Move Variables into Test Fixture
Refactor Test Code:Use Test Fixture
Refactor Test Code:Extract Helper Method
Refactor test code to express the scenario
Let’s Make a Fake
• Stub the method
• Record number of calls
• Fake return value
• Capture arguments
FakeCook
Dependency Injection
• Extract and Override
• Method Injection
• Property Injection
• Constructor Injection
http://www.flickr.com/photos/8499561@N02/2755481069
http://www.flickr.com/photos/jsmoorman/2298671281
3 Types ofUnit Tests:
Return Value Test
State Test
Interaction Test
QualityCoding.org @qcoding
• Working Effectively with Legacy Code by Michael Feathers
• Dependency Injection in .NET by Mark Seemann
• The Art of Unit Testing by Roy Osherove
Resources