Slide 1

Slide 1 text

Unit Testing:
 Control Your Dependencies! Jon Reid — @qcoding
 Facebook

Slide 2

Slide 2 text

Write a failing test Make the test pass Refactor

Slide 3

Slide 3 text

UI? Networking?

Slide 4

Slide 4 text

UI! Networking!

Slide 5

Slide 5 text

http://www.flickr.com/photos/jsmoorman/2298671281 3 Types of
 Unit Tests: ! Return Value Test ! State Test ! Interaction Test

Slide 6

Slide 6 text

Return Value Test Arrange: Set up object Act: Call method that returns a value Assert: Compare against expected value Test SUT call method value System Under Test

Slide 7

Slide 7 text

State Test Arrange: Set up object Act: Call method Assert: Query object in some way Test SUT call method result query

Slide 8

Slide 8 text

Interaction Test Test SUT Something Else! ? ?

Slide 9

Slide 9 text

Interaction Test Table GetDishes Waiter Cook ProcessOrder

Slide 10

Slide 10 text

Interaction Test Table GetDishes Waiter Cook ProcessOrder Fake Cook

Slide 11

Slide 11 text

Dependency Injection Table GetDishes Waiter Cook ProcessOrder Fake Cook

Slide 12

Slide 12 text

Dependency Injection • Extract and Override • Method Injection • Property Injection • Constructor Injection http://www.flickr.com/photos/8499561@N02/2755481069

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Seams http://www.flickr.com/photos/gileslane/2262575575

Slide 15

Slide 15 text

Extract and Override

Slide 16

Slide 16 text

Extract and Override

Slide 17

Slide 17 text

Method Injection

Slide 18

Slide 18 text

Method Injection

Slide 19

Slide 19 text

Property Injection

Slide 20

Slide 20 text

Property Injection

Slide 21

Slide 21 text

Property Injection

Slide 22

Slide 22 text

Constructor Injection

Slide 23

Slide 23 text

Dependency Injection • Extract and Override • Method Injection • Property Injection • Constructor Injection • Ambient Context http://www.flickr.com/photos/8499561@N02/2755481069

Slide 24

Slide 24 text

Dependency Injection M A N N I N G Mark Seemann FOREWORD BY GLENN BLOCK in .NET

Slide 25

Slide 25 text

Interaction Test SUT Real Thing SUT Fake Thing Production Testing ?

Slide 26

Slide 26 text

Types of Fakes M A N N I N G the art of with examples in C# FOREWORDS BY Michael Feathers Robert C. Martin SECOND EDITION ROY OSHEROVE

Slide 27

Slide 27 text

Stub Test SUT Stub communicate assert (Fake)

Slide 28

Slide 28 text

Mock Test SUT Mock communicate assert (Fake)

Slide 29

Slide 29 text

“That’s great, Jon. …But how do I use this for networking?”

Slide 30

Slide 30 text

Networking Call Inject dependency

Slide 31

Slide 31 text

Networking Call Record number of calls

Slide 32

Slide 32 text

Networking Call Fake return value

Slide 33

Slide 33 text

Networking Call Capture arguments

Slide 34

Slide 34 text

Let’s Make a Fake

Slide 35

Slide 35 text

Let’s Make a Fake: 1. Stub the method

Slide 36

Slide 36 text

Let’s Make a Fake: 2. Record number of calls

Slide 37

Slide 37 text

Let’s Make a Fake: 3. Fake return value

Slide 38

Slide 38 text

Let’s Make a Fake: 4. Capture arguments

Slide 39

Slide 39 text

Let’s Write a Test (1): Does it call GET exactly once? 1. Arrange ! 2. Act
 3. Assert

Slide 40

Slide 40 text

Let’s Write a Test (2): Simulate a JSON Response!

Slide 41

Slide 41 text

Refactor Test Code: Spot Duplication

Slide 42

Slide 42 text

Refactor Test Code: Move Variables into Test Fixture

Slide 43

Slide 43 text

Refactor Test Code: Use Test Fixture

Slide 44

Slide 44 text

Refactor Test Code: Extract Helper Method Refactor test code to express the scenario

Slide 45

Slide 45 text

Let’s Make a Fake • Stub the method • Record number of calls • Fake return value • Capture arguments Fake Cook

Slide 46

Slide 46 text

Dependency Injection • Extract and Override • Method Injection • Property Injection • Constructor Injection http://www.flickr.com/photos/8499561@N02/2755481069

Slide 47

Slide 47 text

http://www.flickr.com/photos/jsmoorman/2298671281 3 Types of
 Unit Tests: ! Return Value Test ! State Test ! Interaction Test

Slide 48

Slide 48 text

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 • Test-Driven iOS Development by Graham Lee Resources