Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Unit Testing: Control Your Dependencies!

E1e214e84e1379daf4358ed1ffd17231?s=47 Jon Reid
September 18, 2014

Unit Testing: Control Your Dependencies!

How to turn dependencies around so you can write code that is testable. Includes an example of testing networking code.

E1e214e84e1379daf4358ed1ffd17231?s=128

Jon Reid

September 18, 2014
Tweet

Other Decks in Programming

Transcript

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

  2. Write a failing test Make the test pass Refactor

  3. UI? Networking?

  4. UI! Networking!

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

    ! State Test ! Interaction Test
  6. 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
  7. State Test Arrange: Set up object Act: Call method Assert:

    Query object in some way Test SUT call method result query
  8. Interaction Test Test SUT Something Else! ? ?

  9. Interaction Test Table GetDishes Waiter Cook ProcessOrder

  10. Interaction Test Table GetDishes Waiter Cook ProcessOrder Fake Cook

  11. Dependency Injection Table GetDishes Waiter Cook ProcessOrder Fake Cook

  12. Dependency Injection • Extract and Override • Method Injection •

    Property Injection • Constructor Injection http://www.flickr.com/photos/8499561@N02/2755481069
  13. None
  14. Seams http://www.flickr.com/photos/gileslane/2262575575

  15. Extract and Override

  16. Extract and Override

  17. Method Injection

  18. Method Injection

  19. Property Injection

  20. Property Injection

  21. Property Injection

  22. Constructor Injection

  23. Dependency Injection • Extract and Override • Method Injection •

    Property Injection • Constructor Injection • Ambient Context http://www.flickr.com/photos/8499561@N02/2755481069
  24. Dependency Injection M A N N I N G Mark

    Seemann FOREWORD BY GLENN BLOCK in .NET
  25. Interaction Test SUT Real Thing SUT Fake Thing Production Testing

    ?
  26. 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
  27. Stub Test SUT Stub communicate assert (Fake)

  28. Mock Test SUT Mock communicate assert (Fake)

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

    networking?”
  30. Networking Call Inject dependency

  31. Networking Call Record number of calls

  32. Networking Call Fake return value

  33. Networking Call Capture arguments

  34. Let’s Make a Fake

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

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

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

  38. Let’s Make a Fake: 4. Capture arguments

  39. Let’s Write a Test (1): Does it call GET exactly

    once? 1. Arrange ! 2. Act
 3. Assert
  40. Let’s Write a Test (2): Simulate a JSON Response!

  41. Refactor Test Code: Spot Duplication

  42. Refactor Test Code: Move Variables into Test Fixture

  43. Refactor Test Code: Use Test Fixture

  44. Refactor Test Code: Extract Helper Method Refactor test code to

    express the scenario
  45. Let’s Make a Fake • Stub the method • Record

    number of calls • Fake return value • Capture arguments Fake Cook
  46. Dependency Injection • Extract and Override • Method Injection •

    Property Injection • Constructor Injection http://www.flickr.com/photos/8499561@N02/2755481069
  47. http://www.flickr.com/photos/jsmoorman/2298671281 3 Types of
 Unit Tests: ! Return Value Test

    ! State Test ! Interaction Test
  48. 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