Slide 1

Slide 1 text

@sandimetz Apr 2013 The Magic Tricks of Testing Sandi Metz Friday, April 5, 13

Slide 2

Slide 2 text

@sandimetz Apr 2013 Why do I hate my tests? Friday, April 5, 13

Slide 3

Slide 3 text

@sandimetz Apr 2013 Why do I hate my tests? Friday, April 5, 13

Slide 4

Slide 4 text

@sandimetz Apr 2013 They’re slow. Friday, April 5, 13

Slide 5

Slide 5 text

@sandimetz Apr 2013 They break every time I change something. Friday, April 5, 13

Slide 6

Slide 6 text

@sandimetz Apr 2013 They cost more than they’re worth. Friday, April 5, 13

Slide 7

Slide 7 text

@sandimetz Apr 2013 They are misery incarnate. Friday, April 5, 13

Slide 8

Slide 8 text

@sandimetz Apr 2013 It doesn’t have to be this way. Friday, April 5, 13

Slide 9

Slide 9 text

@sandimetz Apr 2013 You just need to delete some tests. Friday, April 5, 13

Slide 10

Slide 10 text

@sandimetz Apr 2013 Unit Tests: Goals Friday, April 5, 13

Slide 11

Slide 11 text

@sandimetz Apr 2013 Thorough Unit Tests: Goals Friday, April 5, 13

Slide 12

Slide 12 text

@sandimetz Apr 2013 Thorough Stable Unit Tests: Goals Friday, April 5, 13

Slide 13

Slide 13 text

@sandimetz Apr 2013 Thorough Stable Fast Unit Tests: Goals Friday, April 5, 13

Slide 14

Slide 14 text

@sandimetz Apr 2013 Thorough Stable Fast Few Unit Tests: Goals Friday, April 5, 13

Slide 15

Slide 15 text

@sandimetz Apr 2013 What does your app feel like? Friday, April 5, 13

Slide 16

Slide 16 text

@sandimetz Apr 2013 Friday, April 5, 13

Slide 17

Slide 17 text

@sandimetz Apr 2013 Focus on messages. Friday, April 5, 13

Slide 18

Slide 18 text

@sandimetz Apr 2013 Objects have a simple understanding of messages. Friday, April 5, 13

Slide 19

Slide 19 text

@sandimetz Apr 2013 Object Under Test Friday, April 5, 13

Slide 20

Slide 20 text

@sandimetz Apr 2013 Friday, April 5, 13

Slide 21

Slide 21 text

@sandimetz Apr 2013 Object Under Test Friday, April 5, 13

Slide 22

Slide 22 text

@sandimetz Apr 2013 Object Under Test Received from others Friday, April 5, 13

Slide 23

Slide 23 text

@sandimetz Apr 2013 Object Under Test Received from others Friday, April 5, 13

Slide 24

Slide 24 text

@sandimetz Apr 2013 Object Under Test Incoming Friday, April 5, 13

Slide 25

Slide 25 text

@sandimetz Apr 2013 Sent to others Incoming Object Under Test Friday, April 5, 13

Slide 26

Slide 26 text

@sandimetz Apr 2013 Sent to others Incoming Object Under Test Friday, April 5, 13

Slide 27

Slide 27 text

@sandimetz Apr 2013 Incoming Outgoing Object Under Test Friday, April 5, 13

Slide 28

Slide 28 text

@sandimetz Apr 2013 Sent to self Incoming Outgoing Object Under Test Friday, April 5, 13

Slide 29

Slide 29 text

@sandimetz Apr 2013 Sent to self Incoming Outgoing Object Under Test Friday, April 5, 13

Slide 30

Slide 30 text

@sandimetz Apr 2013 Incoming Sent to Self Outgoing Message Origin Friday, April 5, 13

Slide 31

Slide 31 text

@sandimetz Apr 2013 Sent to self Incoming Outgoing Object Under Test Friday, April 5, 13

Slide 32

Slide 32 text

@sandimetz Apr 2013 Sent to self Incoming Outgoing Object Under Test Friday, April 5, 13

Slide 33

Slide 33 text

@sandimetz Apr 2013 Sent to self Incoming Outgoing Object Under Test Friday, April 5, 13

Slide 34

Slide 34 text

@sandimetz Apr 2013 Sent to self Incoming Outgoing Query Command Object Under Test Friday, April 5, 13

Slide 35

Slide 35 text

@sandimetz Apr 2013 Query Command Message Type Friday, April 5, 13

Slide 36

Slide 36 text

@sandimetz Apr 2013 Query: Returns something but changes nothing. Command: Returns nothing but changes something. Type Friday, April 5, 13

Slide 37

Slide 37 text

@sandimetz Apr 2013 Query: Returns something but changes nothing. Command: Returns nothing but changes something. Type Friday, April 5, 13

Slide 38

Slide 38 text

@sandimetz Apr 2013 Query: Returns something but changes nothing. Command: Returns nothing but changes something. Type Friday, April 5, 13

Slide 39

Slide 39 text

@sandimetz Apr 2013 We conflate commands and queries at our peril. Friday, April 5, 13

Slide 40

Slide 40 text

@sandimetz Apr 2013 But we do it all the time. Friday, April 5, 13

Slide 41

Slide 41 text

@sandimetz Apr 2013 3 message Origins X 2 message Types Friday, April 5, 13

Slide 42

Slide 42 text

Query Command Message Origin x Type Outgoing Incoming Type POV @sandimetz Apr 2013 Message Sent to Self Friday, April 5, 13

Slide 43

Slide 43 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming Part 1 Incoming Friday, April 5, 13

Slide 44

Slide 44 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming Incoming Query Messages Incoming Friday, April 5, 13

Slide 45

Slide 45 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming Incoming Wheel diameter Friday, April 5, 13

Slide 46

Slide 46 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  Wheel    attr_reader  :rim,  :tire    def  initialize(rim,  tire)    #  ...    end    def  diameter        rim  +  (tire  *  2)    end    #  ... Friday, April 5, 13

Slide 47

Slide 47 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  Wheel    attr_reader  :rim,  :tire    def  initialize(rim,  tire)    #  ...    end    def  diameter        rim  +  (tire  *  2)    end    #  ... Friday, April 5, 13

Slide 48

Slide 48 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  Wheel    attr_reader  :rim,  :tire    def  initialize(rim,  tire)    #  ...    end    def  diameter        rim  +  (tire  *  2)    end    #  ... Friday, April 5, 13

Slide 49

Slide 49 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  WheelTest  <  MiniTest::Unit::TestCase    def  test_calculates_diameter        wheel  =  Wheel.new(26,  1.5)        assert_in_delta(29,                                        wheel.diameter,                                        0.01)    end end Friday, April 5, 13

Slide 50

Slide 50 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  WheelTest  <  MiniTest::Unit::TestCase    def  test_calculates_diameter        wheel  =  Wheel.new(26,  1.5)        assert_in_delta(29,                                        wheel.diameter,                                        0.01)    end end Friday, April 5, 13

Slide 51

Slide 51 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  WheelTest  <  MiniTest::Unit::TestCase    def  test_calculates_diameter        wheel  =  Wheel.new(26,  1.5)        assert_in_delta(29,                                        wheel.diameter,                                        0.01)    end end Friday, April 5, 13

Slide 52

Slide 52 text

@sandimetz Apr 2013 Rule Friday, April 5, 13

Slide 53

Slide 53 text

@sandimetz Apr 2013 Test incoming query messages Rule Friday, April 5, 13

Slide 54

Slide 54 text

@sandimetz Apr 2013 Test incoming query messages by making assertions about what they send back. Rule Friday, April 5, 13

Slide 55

Slide 55 text

Query Command The Unit Testing Minimalist Incoming Type POV @sandimetz Apr 2013 Message Sent to Self Outgoing Friday, April 5, 13

Slide 56

Slide 56 text

Query Command Assert result The Unit Testing Minimalist Incoming Type POV @sandimetz Apr 2013 Message Sent to Self Outgoing Friday, April 5, 13

Slide 57

Slide 57 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming Incoming Another Incoming Query Message Friday, April 5, 13

Slide 58

Slide 58 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming Gear gear_inches Incoming Friday, April 5, 13

Slide 59

Slide 59 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  Gear    attr_reader  :chainring,  :cog,  :wheel    def  initialize(args)        #  ...    end      #  ...    def  gear_inches        ratio  *  wheel.diameter    end      private    def  ratio        chainring  /  cog.to_f    end      #  ... end Friday, April 5, 13

Slide 60

Slide 60 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  Gear    attr_reader  :chainring,  :cog,  :wheel    def  initialize(args)        #  ...    end      #  ...    def  gear_inches        ratio  *  wheel.diameter    end      private    def  ratio        chainring  /  cog.to_f    end      #  ... end Friday, April 5, 13

Slide 61

Slide 61 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  Gear    attr_reader  :chainring,  :cog,  :wheel    def  initialize(args)        #  ...    end      #  ...    def  gear_inches        ratio  *  wheel.diameter    end      private    def  ratio        chainring  /  cog.to_f    end      #  ... end Friday, April 5, 13

Slide 62

Slide 62 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  Gear    attr_reader  :chainring,  :cog,  :wheel    def  initialize(args)        #  ...    end      #  ...    def  gear_inches        ratio  *  wheel.diameter    end      private    def  ratio        chainring  /  cog.to_f    end      #  ... end Friday, April 5, 13

Slide 63

Slide 63 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  Gear    attr_reader  :chainring,  :cog,  :wheel    def  initialize(args)        #  ...    end      #  ...    def  gear_inches        ratio  *  wheel.diameter    end      private    def  ratio        chainring  /  cog.to_f    end      #  ... end Wheel Friday, April 5, 13

Slide 64

Slide 64 text

@sandimetz Apr 2013 Sight along the edges of the space capsule. Friday, April 5, 13

Slide 65

Slide 65 text

@sandimetz Apr 2013 Sight along the edges of the space capsule. Friday, April 5, 13

Slide 66

Slide 66 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  GearTest  <  MiniTest::Unit::TestCase    def  test_calculates_gear_inches        gear  =    Gear.new(                            chainring:  52,                            cog:              11,                            wheel:          Wheel.new(26,  1.5))        assert_in_delta(137.1,                                        gear.gear_inches,                                        0.01)    end end Friday, April 5, 13

Slide 67

Slide 67 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  GearTest  <  MiniTest::Unit::TestCase    def  test_calculates_gear_inches        gear  =    Gear.new(                            chainring:  52,                            cog:              11,                            wheel:          Wheel.new(26,  1.5))        assert_in_delta(137.1,                                        gear.gear_inches,                                        0.01)    end end Friday, April 5, 13

Slide 68

Slide 68 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  GearTest  <  MiniTest::Unit::TestCase    def  test_calculates_gear_inches        gear  =    Gear.new(                            chainring:  52,                            cog:              11,                            wheel:          Wheel.new(26,  1.5))        assert_in_delta(137.1,                                        gear.gear_inches,                                        0.01)    end end Friday, April 5, 13

Slide 69

Slide 69 text

@sandimetz Apr 2013 Test the interface, Friday, April 5, 13

Slide 70

Slide 70 text

@sandimetz Apr 2013 Test the interface, not the implementation. Friday, April 5, 13

Slide 71

Slide 71 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming Incoming Command Messages Incoming Friday, April 5, 13

Slide 72

Slide 72 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming Gear set_cog Incoming Friday, April 5, 13

Slide 73

Slide 73 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  Gear  attr_reader  :chainring,  :cog,  :wheel    def  initialize(args)        #  ...    end    def  set_cog(new_cog)        @cog  =  new_cog    end end Friday, April 5, 13

Slide 74

Slide 74 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  Gear  attr_reader  :chainring,  :cog,  :wheel    def  initialize(args)        #  ...    end    def  set_cog(new_cog)        @cog  =  new_cog    end end Friday, April 5, 13

Slide 75

Slide 75 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  Gear  attr_reader  :chainring,  :cog,  :wheel    def  initialize(args)        #  ...    end    def  set_cog(new_cog)        @cog  =  new_cog    end end Friday, April 5, 13

Slide 76

Slide 76 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  GearTest  <  MiniTest::Unit::TestCase    def  test_set_cog        gear  =  Gear.new        gear.set_cog(27)        assert(27,  gear.cog)    end end Friday, April 5, 13

Slide 77

Slide 77 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  GearTest  <  MiniTest::Unit::TestCase    def  test_set_cog        gear  =  Gear.new        gear.set_cog(27)        assert(27,  gear.cog)    end end Friday, April 5, 13

Slide 78

Slide 78 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  GearTest  <  MiniTest::Unit::TestCase    def  test_set_cog        gear  =  Gear.new        gear.set_cog(27)        assert(27,  gear.cog)    end end Send the message Friday, April 5, 13

Slide 79

Slide 79 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  GearTest  <  MiniTest::Unit::TestCase    def  test_set_cog        gear  =  Gear.new        gear.set_cog(27)        assert(27,  gear.cog)    end end Assert the side effect Friday, April 5, 13

Slide 80

Slide 80 text

@sandimetz Apr 2013 Rule Friday, April 5, 13

Slide 81

Slide 81 text

@sandimetz Apr 2013 Test incoming command messages Rule Friday, April 5, 13

Slide 82

Slide 82 text

@sandimetz Apr 2013 Test incoming command messages by making assertions about direct public side effects. Rule Friday, April 5, 13

Slide 83

Slide 83 text

Query Command Assert result The Unit Testing Minimalist Incoming Type POV @sandimetz Apr 2013 Message Sent to Self Outgoing Friday, April 5, 13

Slide 84

Slide 84 text

Query Command Assert result The Unit Testing Minimalist Incoming Type POV @sandimetz Apr 2013 Message Assert direct public side effects Sent to Self Outgoing Friday, April 5, 13

Slide 85

Slide 85 text

@sandimetz Apr 2013 DRY It Out Friday, April 5, 13

Slide 86

Slide 86 text

@sandimetz Apr 2013 Receiver of incoming message has sole responsibility DRY It Out Friday, April 5, 13

Slide 87

Slide 87 text

@sandimetz Apr 2013 Receiver of incoming message has sole responsibility for asserting the result direct public side effects. DRY It Out Friday, April 5, 13

Slide 88

Slide 88 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming Part 2 to Self Incoming Friday, April 5, 13

Slide 89

Slide 89 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming to Self Incoming Messages Sent to Self Friday, April 5, 13

Slide 90

Slide 90 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  Gear      #  ...    def  gear_inches        ratio  *  wheel.diameter    end      private    def  ratio        chainring  /  cog.to_f    end      #  ... end Friday, April 5, 13

Slide 91

Slide 91 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming Anti-Pattern to Self Incoming Friday, April 5, 13

Slide 92

Slide 92 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  GearTest  <  MiniTest::Unit::TestCase    def  test_calculates_ratio        gear  =    Gear.new(                            chainring:  52,                            cog:              11,                            wheel:          Wheel.new(26,  1.5))        assert_in_delta(4.7,                                        gear.ratio,                                        0.01)    end Friday, April 5, 13

Slide 93

Slide 93 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  GearTest  <  MiniTest::Unit::TestCase    def  test_calculates_ratio        gear  =    Gear.new(                            chainring:  52,                            cog:              11,                            wheel:          Wheel.new(26,  1.5))        assert_in_delta(4.7,                                        gear.ratio,                                        0.01)    end Test the private method? Friday, April 5, 13

Slide 94

Slide 94 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  GearTest  <  MiniTest::Unit::TestCase    def  test_calculates_ratio        gear  =    Gear.new(                            chainring:  52,                            cog:              11,                            wheel:          Wheel.new(26,  1.5))        assert_in_delta(4.7,                                        gear.ratio,                                        0.01)    end Friday, April 5, 13

Slide 95

Slide 95 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  GearTest  <  MiniTest::Unit::TestCase    def  test_calculates_ratio        gear  =    Gear.new(                            chainring:  52,                            cog:              11,                            wheel:          Wheel.new(26,  1.5))        assert_in_delta(4.7,                                        gear.ratio,                                        0.01)    end Friday, April 5, 13

Slide 96

Slide 96 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  GearTest  <  MiniTest::Unit::TestCase    def  test_calculates_ratio        gear  =    Gear.new(                            chainring:  52,                            cog:              11,                            wheel:          Wheel.new(26,  1.5))        assert_in_delta(4.7,                                        gear.ratio,                                        0.01)    end Redundant: Other tests already prove Gear works. Friday, April 5, 13

Slide 97

Slide 97 text

Friday, April 5, 13

Slide 98

Slide 98 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming Anti-Pattern to Self Incoming Friday, April 5, 13

Slide 99

Slide 99 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  Gear      #  ...    def  gear_inches        ratio  *  wheel.diameter    end      private    def  ratio        chainring  /  cog.to_f    end      #  ... end Friday, April 5, 13

Slide 100

Slide 100 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  Gear      #  ...    def  gear_inches        ratio  *  wheel.diameter    end      private    def  ratio        chainring  /  cog.to_f    end      #  ... end Friday, April 5, 13

Slide 101

Slide 101 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  Gear      #  ...    def  gear_inches        ratio  *  wheel.diameter    end      private    def  ratio        chainring  /  cog.to_f    end      #  ... end Friday, April 5, 13

Slide 102

Slide 102 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  GearTest  <  MiniTest::Unit::TestCase    def  test_calculates_gear_inches        gear  =    Gear.new(                            chainring:  52,                            cog:              11,                            wheel:          Wheel.new(26,  1.5))        assert_in_delta(137.1,                                        gear.gear_inches,                                        0.01)    end end Friday, April 5, 13

Slide 103

Slide 103 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  GearTest  <  MiniTest::Unit::TestCase    def  test_calculates_gear_inches        gear  =    Gear.new(                            chainring:  52,                            cog:              11,                            wheel:          Wheel.new(26,  1.5))        assert_in_delta(137.1,                                        gear.gear_inches,                                        0.01)        gear.expect(:ratio)        gear.verify    end Friday, April 5, 13

Slide 104

Slide 104 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  GearTest  <  MiniTest::Unit::TestCase    def  test_calculates_gear_inches        gear  =    Gear.new(                            chainring:  52,                            cog:              11,                            wheel:          Wheel.new(26,  1.5))        assert_in_delta(137.1,                                        gear.gear_inches,                                        0.01)        gear.expect(:ratio)        gear.verify    end Expect to send the private method? Friday, April 5, 13

Slide 105

Slide 105 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  GearTest  <  MiniTest::Unit::TestCase    def  test_calculates_gear_inches        gear  =    Gear.new(                            chainring:  52,                            cog:              11,                            wheel:          Wheel.new(26,  1.5))        assert_in_delta(137.1,                                        gear.gear_inches,                                        0.01)        gear.expect(:ratio)        gear.verify    end Over Speci ed: Adds no safety yet breaks every time the implementation changes. Friday, April 5, 13

Slide 106

Slide 106 text

Friday, April 5, 13

Slide 107

Slide 107 text

@sandimetz Apr 2013 Rule Friday, April 5, 13

Slide 108

Slide 108 text

@sandimetz Apr 2013 Do not test private methods. Rule Friday, April 5, 13

Slide 109

Slide 109 text

@sandimetz Apr 2013 Do not test private methods. Do not make assertions about their result. Rule Friday, April 5, 13

Slide 110

Slide 110 text

@sandimetz Apr 2013 Do not test private methods. Do not make assertions about their result. Do not expect to send them. Rule Friday, April 5, 13

Slide 111

Slide 111 text

@sandimetz Apr 2013 Temporarily breaking this rule can save $$$ during development. Caveat Friday, April 5, 13

Slide 112

Slide 112 text

Query Command Assert result Assert direct public side-effects The Unit Testing Minimalist Incoming Type POV @sandimetz Apr 2013 Message Sent to Self Outgoing Friday, April 5, 13

Slide 113

Slide 113 text

Query Command Assert result Assert direct public side-effects The Unit Testing Minimalist Incoming Type POV @sandimetz Apr 2013 Ignore Message Sent to Self Outgoing Friday, April 5, 13

Slide 114

Slide 114 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming Part 3 Outgoing to Self Incoming Friday, April 5, 13

Slide 115

Slide 115 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming Outgoing to Self Incoming Outgoing Query Messages Friday, April 5, 13

Slide 116

Slide 116 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming Outgoing to Self Incoming Spoiler Alert Same rules as ‘Sent to Self’ Friday, April 5, 13

Slide 117

Slide 117 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming diameter Gear gear_inches Wheel Outgoing to Self Incoming Friday, April 5, 13

Slide 118

Slide 118 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  Gear    attr_reader  :chainring,  :cog,  :wheel    def  initialize(args)          #  ...    end    def  gear_inches        ratio  *  wheel.diameter    end      private    def  ratio        chainring  /  cog.to_f    end      #  ... end Outgoing to Self Incoming Friday, April 5, 13

Slide 119

Slide 119 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming class  Gear    attr_reader  :chainring,  :cog,  :wheel    def  initialize(args)          #  ...    end    def  gear_inches        ratio  *  wheel.diameter    end      private    def  ratio        chainring  /  cog.to_f    end      #  ... end Outgoing to Self Incoming Wheel Friday, April 5, 13

Slide 120

Slide 120 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming Gear Gear Outgoing Wheel diameter Incoming Query Outgoing to Self Incoming gear_inches Friday, April 5, 13

Slide 121

Slide 121 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming Anti-Pattern to Self Incoming Outgoing to Self Incoming Friday, April 5, 13

Slide 122

Slide 122 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  GearTest  <  MiniTest::Unit::TestCase    def  test_calculates_gear_inches        gear  =    Gear.new(                            chainring:  52,                            cog:              11,                            wheel:          Wheel.new(26,  1.5))        assert_in_delta(137.1,                                        gear.gear_inches,                                        0.01)    end end Friday, April 5, 13

Slide 123

Slide 123 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  GearTest  <  MiniTest::Unit::TestCase    def  test_calculates_gear_inches        gear  =    Gear.new(                            chainring:  52,                            cog:              11,                            wheel:          Wheel.new(26,  1.5))        assert_in_delta(137.1,                                        gear.gear_inches,                                        0.01)          assert_in_delta(29,                                        gear.wheel.diameter,                                        0.01)    end end Friday, April 5, 13

Slide 124

Slide 124 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  GearTest  <  MiniTest::Unit::TestCase    def  test_calculates_gear_inches        gear  =    Gear.new(                            chainring:  52,                            cog:              11,                            wheel:          Wheel.new(26,  1.5))        assert_in_delta(137.1,                                        gear.gear_inches,                                        0.01)          assert_in_delta(29,                                        gear.wheel.diameter,                                        0.01)    end end Assert result of outgoing query? Friday, April 5, 13

Slide 125

Slide 125 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  GearTest  <  MiniTest::Unit::TestCase    def  test_calculates_gear_inches        gear  =    Gear.new(                            chainring:  52,                            cog:              11,                            wheel:          Wheel.new(26,  1.5))        assert_in_delta(137.1,                                        gear.gear_inches,                                        0.01)          assert_in_delta(29,                                        gear.wheel.diameter,                                        0.01)    end end Redundant: This duplicates Wheel’s tests. Friday, April 5, 13

Slide 126

Slide 126 text

Friday, April 5, 13

Slide 127

Slide 127 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming Anti-Pattern to Self Incoming Outgoing to Self Incoming Friday, April 5, 13

Slide 128

Slide 128 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  GearTest  <  MiniTest::Unit::TestCase    def  test_calculates_gear_inches        gear  =    Gear.new(                            chainring:  52,                            cog:              11,                            wheel:          Wheel.new(26,  1.5))        assert_in_delta(137.1,                                        gear.gear_inches,                                        0.01)          gear.wheel.expect(:diameter)        gear.verify    end end Friday, April 5, 13

Slide 129

Slide 129 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  GearTest  <  MiniTest::Unit::TestCase    def  test_calculates_gear_inches        gear  =    Gear.new(                            chainring:  52,                            cog:              11,                            wheel:          Wheel.new(26,  1.5))        assert_in_delta(137.1,                                        gear.gear_inches,                                        0.01)          gear.wheel.expect(:diameter)        gear.verify    end end Expect to send outgoing query? Friday, April 5, 13

Slide 130

Slide 130 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  GearTest  <  MiniTest::Unit::TestCase    def  test_calculates_gear_inches        gear  =    Gear.new(                            chainring:  52,                            cog:              11,                            wheel:          Wheel.new(26,  1.5))        assert_in_delta(137.1,                                        gear.gear_inches,                                        0.01)          gear.wheel.expect(:diameter)        gear.verify    end end Over Speci ed: Adds no safety yet breaks every time the implementation changes. Friday, April 5, 13

Slide 131

Slide 131 text

Friday, April 5, 13

Slide 132

Slide 132 text

@sandimetz Apr 2013 Rule Friday, April 5, 13

Slide 133

Slide 133 text

@sandimetz Apr 2013 Rule Do not test outgoing query messages. Friday, April 5, 13

Slide 134

Slide 134 text

@sandimetz Apr 2013 Rule Do not test outgoing query messages. Do not make assertions about their result. Friday, April 5, 13

Slide 135

Slide 135 text

@sandimetz Apr 2013 Rule Do not test outgoing query messages. Do not make assertions about their result. Do not expect to send them. Friday, April 5, 13

Slide 136

Slide 136 text

@sandimetz Apr 2013 If the message has no visible side-effects Friday, April 5, 13

Slide 137

Slide 137 text

@sandimetz Apr 2013 If the message has no visible side-effects the sender should not test it. Friday, April 5, 13

Slide 138

Slide 138 text

Query Command Assert result Assert direct public side effects The Unit Testing Minimalist Incoming Type POV @sandimetz Apr 2013 Message Ignore Sent to Self Outgoing Friday, April 5, 13

Slide 139

Slide 139 text

Query Command Assert result Assert direct public side effects The Unit Testing Minimalist Incoming Type POV @sandimetz Apr 2013 Message Ignore Sent to Self Outgoing Friday, April 5, 13

Slide 140

Slide 140 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming Outgoing Command Messages to Self Incoming Outgoing Friday, April 5, 13

Slide 141

Slide 141 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming diameter gear_inches set_cog changed to Self Incoming Outgoing Wheel Gear Obs Friday, April 5, 13

Slide 142

Slide 142 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  Gear    attr_reader  :chainring,  :cog,  :wheel    def  initialize(args)              #  ...      end    def  set_cog(new_cog)        @cog  =  new_cog    end end Change #set_cog to add observer. Friday, April 5, 13

Slide 143

Slide 143 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  Gear    attr_reader  :chainring,  :cog,  :wheel    def  initialize(args)              #  ...      end    def  set_cog(new_cog)        @cog  =  new_cog    end end Friday, April 5, 13

Slide 144

Slide 144 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  Gear    attr_reader  :chainring,  :cog,  :wheel,  :observer    def  initialize(args)              #  ...        @observer    =  args[:observer]    end    def  set_cog(new_cog)        @cog  =  new_cog    end end Friday, April 5, 13

Slide 145

Slide 145 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  Gear      #  ...    def  set_cog(new_cog)        @cog  =  new_cog    end end Friday, April 5, 13

Slide 146

Slide 146 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  Gear      #  ...    def  set_cog(new_cog)        @cog  =  new_cog    end end Friday, April 5, 13

Slide 147

Slide 147 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  Gear      #  ...    def  set_cog(new_cog)        @cog  =  new_cog        changed        @cog    end    def  changed        observer.changed(chainring,  cog)    end end Friday, April 5, 13

Slide 148

Slide 148 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  Gear      #  ...    def  set_cog(new_cog)        @cog  =  new_cog        changed        @cog    end    def  changed        observer.changed(chainring,  cog)    end end Friday, April 5, 13

Slide 149

Slide 149 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming Gear set_cog changed Obs to Self Incoming Outgoing Friday, April 5, 13

Slide 150

Slide 150 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming Gear set_cog changed Obs to Self Incoming Outgoing Side Effects Friday, April 5, 13

Slide 151

Slide 151 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming changed Gear set_cog Obs Incoming Command to Self Incoming Outgoing Side Effects Friday, April 5, 13

Slide 152

Slide 152 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming Gear Outgoing set_cog Obs changed Incoming Command to Self Incoming Outgoing Side Effects Friday, April 5, 13

Slide 153

Slide 153 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  GearTest  <  MiniTest::Unit::TestCase    def  test_set_cog        gear  =  Gear.new        gear.set_cog(27)        assert(27,  gear.cog)    end end Make existing #set_cog test run. Friday, April 5, 13

Slide 154

Slide 154 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  GearTest  <  MiniTest::Unit::TestCase    def  test_set_cog        gear  =  Gear.new                                                                                                                    @gear.set_cog(27)        assert(27,  @gear.cog)    end end Friday, April 5, 13

Slide 155

Slide 155 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  GearTest  <  MiniTest::Unit::TestCase    def  test_set_cog        @observer  =  Obs.new        gear  =  Gear.new                                                                                                                    @gear.set_cog(27)        assert(27,  @gear.cog)    end end Friday, April 5, 13

Slide 156

Slide 156 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  GearTest  <  MiniTest::Unit::TestCase    def  test_set_cog        @observer  =  Obs.new        @gear          =  Gear.new(                                    chainring:  52,                                    cog:              11,                                    observer:    @observer)        @gear.set_cog(27)        assert(27,  @gear.cog)    end end Friday, April 5, 13

Slide 157

Slide 157 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  GearTest  <  MiniTest::Unit::TestCase    def  test_set_cog        @observer  =  Obs.new        @gear          =  Gear.new(                                    chainring:  52,                                    cog:              11,                                    observer:    @observer)        @gear.set_cog(27)        assert(27,  @gear.cog)    end end Runs real @observer#changed 1st version Friday, April 5, 13

Slide 158

Slide 158 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  GearTest  <  MiniTest::Unit::TestCase    def  test_set_cog        @observer  =  Obs.new        @gear          =  Gear.new(                                    chainring:  52,                                    cog:              11,                                    observer:    @observer)        @observer.stub(:changed,  @gear.set_cog(27))  do            assert(27,  @gear.cog)        end    end end Variant Runs fake @observer#changed Friday, April 5, 13

Slide 159

Slide 159 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  Gear        #  ...    def  set_cog(new_cog)        @cog  =  new_cog        changed        @cog    end    def  changed        observer.changed(chainring,  cog)    end end Friday, April 5, 13

Slide 160

Slide 160 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  Gear        #  ...    def  set_cog(new_cog)        @cog  =  new_cog        changed        @cog    end    def  changed        observer.changed(chainring,  cog)    end end The app will be correct only if this message gets sent. Friday, April 5, 13

Slide 161

Slide 161 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 Wheel Incoming Query changed Side Effects Gear Incoming Command Outgoing Obs Outgoing diameter gear_inches set_cog Friday, April 5, 13

Slide 162

Slide 162 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 Side Effects Gear Wheel Obs Friday, April 5, 13

Slide 163

Slide 163 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 Side Effects Gear Wheel Obs Friday, April 5, 13

Slide 164

Slide 164 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 Side Effects Gear Wheel Obs Friday, April 5, 13

Slide 165

Slide 165 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Outgoing Incoming Anti-Pattern to Self Incoming Outgoing Friday, April 5, 13

Slide 166

Slide 166 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  GearTest  <  MiniTest::Unit::TestCase    def  test_saves_changed_cog_in_db        @observer  =  Obs.new        @gear          =  Gear.new(                                    chainring:  52,                                    cog:              11,                                    observer:    @observer)          @gear.set_cog(27)          #  assert  something  about  the  state  of  the  db    end end Friday, April 5, 13

Slide 167

Slide 167 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  GearTest  <  MiniTest::Unit::TestCase    def  test_saves_changed_cog_in_db        @observer  =  Obs.new        @gear          =  Gear.new(                                    chainring:  52,                                    cog:              11,                                    observer:    @observer)          @gear.set_cog(27)          #  assert  something  about  the  state  of  the  db    end end Friday, April 5, 13

Slide 168

Slide 168 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  GearTest  <  MiniTest::Unit::TestCase    def  test_saves_changed_cog_in_db        @observer  =  Obs.new        @gear          =  Gear.new(                                    chainring:  52,                                    cog:              11,                                    observer:    @observer)          @gear.set_cog(27)          #  assert  something  about  the  state  of  the  db    end end Friday, April 5, 13

Slide 169

Slide 169 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  GearTest  <  MiniTest::Unit::TestCase    def  test_saves_changed_cog_in_db        @observer  =  Obs.new        @gear          =  Gear.new(                                    chainring:  52,                                    cog:              11,                                    observer:    @observer)          @gear.set_cog(27)          #  assert  something  about  the  state  of  the  db    end end Depends on the distant side effect. Friday, April 5, 13

Slide 170

Slide 170 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  GearTest  <  MiniTest::Unit::TestCase    def  test_saves_changed_cog_in_db        @observer  =  Obs.new        @gear          =  Gear.new(                                    chainring:  52,                                    cog:              11,                                    observer:    @observer)          @gear.set_cog(27)          #  assert  something  about  the  state  of  the  db    end end Is this Gear’s responsibility? Friday, April 5, 13

Slide 171

Slide 171 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  GearTest  <  MiniTest::Unit::TestCase    def  test_saves_changed_cog_in_db        @observer  =  Obs.new        @gear          =  Gear.new(                                    chainring:  52,                                    cog:              11,                                    observer:    @observer)          @gear.set_cog(27)          #  assert  something  about  the  state  of  the  db    end end Is this Gear’s responsibility? Friday, April 5, 13

Slide 172

Slide 172 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  GearTest  <  MiniTest::Unit::TestCase    def  test_saves_changed_cog_in_db        @observer  =  Obs.new        @gear          =  Gear.new(                                    chainring:  52,                                    cog:              11,                                    observer:    @observer)          @gear.set_cog(27)          #  assert  something  about  the  state  of  the  db    end end This is an integration test. Friday, April 5, 13

Slide 173

Slide 173 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  GearTest  <  MiniTest::Unit::TestCase    def  test_notifies_observers_when_cogs_change        @observer  =  MiniTest::Mock.new        @gear          =  Gear.new(                                    chainring:  52,                                    cog:              11,                                    observer:    @observer)        @observer.expect(:changed,  true,  [52,  27])        @gear.set_cog(27)        @observer.verify    end Friday, April 5, 13

Slide 174

Slide 174 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  GearTest  <  MiniTest::Unit::TestCase    def  test_notifies_observers_when_cogs_change        @observer  =  MiniTest::Mock.new        @gear          =  Gear.new(                                    chainring:  52,                                    cog:              11,                                    observer:    @observer)        @observer.expect(:changed,  true,  [52,  27])        @gear.set_cog(27)        @observer.verify    end Friday, April 5, 13

Slide 175

Slide 175 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  GearTest  <  MiniTest::Unit::TestCase    def  test_notifies_observers_when_cogs_change        @observer  =  MiniTest::Mock.new        @gear          =  Gear.new(                                    chainring:  52,                                    cog:              11,                                    observer:    @observer)        @observer.expect(:changed,  true,  [52,  27])        @gear.set_cog(27)        @observer.verify    end Friday, April 5, 13

Slide 176

Slide 176 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  GearTest  <  MiniTest::Unit::TestCase    def  test_notifies_observers_when_cogs_change        @observer  =  MiniTest::Mock.new        @gear          =  Gear.new(                                    chainring:  52,                                    cog:              11,                                    observer:    @observer)        @observer.expect(:changed,  true,  [52,  27])        @gear.set_cog(27)        @observer.verify    end Friday, April 5, 13

Slide 177

Slide 177 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  GearTest  <  MiniTest::Unit::TestCase    def  test_notifies_observers_when_cogs_change        @observer  =  MiniTest::Mock.new        @gear          =  Gear.new(                                    chainring:  52,                                    cog:              11,                                    observer:    @observer)        @observer.expect(:changed,  true,  [52,  27])        @gear.set_cog(27)        @observer.verify    end Friday, April 5, 13

Slide 178

Slide 178 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  GearTest  <  MiniTest::Unit::TestCase    def  test_notifies_observers_when_cogs_change        @observer  =  MiniTest::Mock.new        @gear          =  Gear.new(                                    chainring:  52,                                    cog:              11,                                    observer:    @observer)        @observer.expect(:changed,  true,  [52,  27])        @gear.set_cog(27)        @observer.verify    end Friday, April 5, 13

Slide 179

Slide 179 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  GearTest  <  MiniTest::Unit::TestCase    def  test_notifies_observers_when_cogs_change        @observer  =  MiniTest::Mock.new        @gear          =  Gear.new(                                    chainring:  52,                                    cog:              11,                                    observer:    @observer)        @observer.expect(:changed,  true,  [52,  27])        @gear.set_cog(27)        @observer.verify    end Depends on the interface. Friday, April 5, 13

Slide 180

Slide 180 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  GearTest  <  MiniTest::Unit::TestCase    def  test_notifies_observers_when_cogs_change        @observer  =  MiniTest::Mock.new        @gear          =  Gear.new(                                    chainring:  52,                                    cog:              11,                                    observer:    @observer)        @observer.expect(:changed,  true,  [52,  27])        @gear.set_cog(27)        @observer.verify    end Gear is responsible for sending #changed to @observer. Friday, April 5, 13

Slide 181

Slide 181 text

@sandimetz Apr 2013 Rule Friday, April 5, 13

Slide 182

Slide 182 text

@sandimetz Apr 2013 Expect to send outgoing command messages. Rule Friday, April 5, 13

Slide 183

Slide 183 text

@sandimetz Apr 2013 Breaking this rule can save $$$ if side effects are both stable and cheap. Caveat Friday, April 5, 13

Slide 184

Slide 184 text

Query Command Assert result Assert direct public side effects Ignore The Unit Testing Minimalist Incoming Type POV @sandimetz Apr 2013 Message Ignore Sent to Self Outgoing Friday, April 5, 13

Slide 185

Slide 185 text

Query Command Assert result Assert direct public side effects Ignore Expect to send The Unit Testing Minimalist Incoming Type POV @sandimetz Apr 2013 Message Ignore Sent to Self Outgoing Friday, April 5, 13

Slide 186

Slide 186 text

@sandimetz Apr 2013 But Friday, April 5, 13

Slide 187

Slide 187 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  GearTest  <  MiniTest::Unit::TestCase    def  test_notifies_observers_when_cogs_change        @observer  =  MiniTest::Mock.new        @gear          =  Gear.new(                                    chainring:  52,                                    cog:              11,                                    observer:    @observer)        @observer.expect(:changed,  true,  [52,  27])        @gear.set_cog(27)        @observer.verify    end What happens if the Observer class stops implementing #changed? Friday, April 5, 13

Slide 188

Slide 188 text

@sandimetz Apr 2013 Stubs Friday, April 5, 13

Slide 189

Slide 189 text

@sandimetz Apr 2013 Stubs Stubs de ne context. Friday, April 5, 13

Slide 190

Slide 190 text

@sandimetz Apr 2013 Stubs Stubs de ne context. Stub as needed (but zillions is a smell). Friday, April 5, 13

Slide 191

Slide 191 text

@sandimetz Apr 2013 Stubs Stubs de ne context. Stub as needed (but zillions is a smell). Avoid stubbing in the object under test. Friday, April 5, 13

Slide 192

Slide 192 text

@sandimetz Apr 2013 Stubs Stubs de ne context. Stub as needed (but zillions is a smell). Avoid stubbing in the object under test. Do not make assertions about stubs! Friday, April 5, 13

Slide 193

Slide 193 text

@sandimetz Apr 2013 Mocks Friday, April 5, 13

Slide 194

Slide 194 text

@sandimetz Apr 2013 Mocks Mocks test behavior. Friday, April 5, 13

Slide 195

Slide 195 text

@sandimetz Apr 2013 Mocks Mocks test behavior. Set expectations on mocks to test outgoing command messages. Friday, April 5, 13

Slide 196

Slide 196 text

@sandimetz Apr 2013 Mocks Mocks test behavior. Set expectations on mocks to test outgoing command messages. One expectation per test (do your best). Friday, April 5, 13

Slide 197

Slide 197 text

@sandimetz Apr 2013 Mocks Mocks test behavior. Set expectations on mocks to test outgoing command messages. One expectation per test (do your best). Do not stub using a mock! Friday, April 5, 13

Slide 198

Slide 198 text

@sandimetz GoGaRuCo 2012 @sandimetz Apr 2013 to Self Incoming Outgoing class  GearTest  <  MiniTest::Unit::TestCase    def  test_notifies_observers_when_cogs_change        @observer  =  MiniTest::Mock.new        @gear          =  Gear.new(                                    chainring:  52,                                    cog:              11,                                    observer:    @observer)        @observer.expect(:changed,  true,  [52,  27])        @gear.set_cog(27)        @observer.verify    end So really, what happens if Observer stops implementing #changed? Friday, April 5, 13

Slide 199

Slide 199 text

@sandimetz Apr 2013 Pain Friday, April 5, 13

Slide 200

Slide 200 text

@sandimetz Apr 2013 API Drift Pain Friday, April 5, 13

Slide 201

Slide 201 text

@sandimetz Apr 2013 Rule Friday, April 5, 13

Slide 202

Slide 202 text

@sandimetz Apr 2013 Honor the contract. Rule Friday, April 5, 13

Slide 203

Slide 203 text

@sandimetz Apr 2013 Honor the contract. Ensure test doubles stay in sync with the API. Rule Friday, April 5, 13

Slide 204

Slide 204 text

@sandimetz Apr 2013 Automagically  minitest  requires  that  stubbed  methods  exist  https://github.com/benmoss/quacky  https://github.com/xaviershay/rspec-­‐fire  https://github.com/cfcosta/minitest-­‐firemock Friday, April 5, 13

Slide 205

Slide 205 text

@sandimetz Apr 2013 Summary Friday, April 5, 13

Slide 206

Slide 206 text

@sandimetz Apr 2013 Friday, April 5, 13

Slide 207

Slide 207 text

@sandimetz Apr 2013 Friday, April 5, 13

Slide 208

Slide 208 text

@sandimetz Apr 2013 Strive for Thorough, Stable Fast, Few Friday, April 5, 13

Slide 209

Slide 209 text

@sandimetz Apr 2013 Test. Friday, April 5, 13

Slide 210

Slide 210 text

@sandimetz Apr 2013 Test. Everything. Friday, April 5, 13

Slide 211

Slide 211 text

@sandimetz Apr 2013 Test . Everything. Once. Friday, April 5, 13

Slide 212

Slide 212 text

@sandimetz Apr 2013 Test the interface, Friday, April 5, 13

Slide 213

Slide 213 text

@sandimetz Apr 2013 Test the interface, not the implementation. Friday, April 5, 13

Slide 214

Slide 214 text

@sandimetz Apr 2013 Guard against API drift. Friday, April 5, 13

Slide 215

Slide 215 text

Query Command Assert result Assert direct public side effects Ignore Expect to send Be a Minimalist Incoming Type POV @sandimetz Apr 2013 Message Ignore Sent to Self Outgoing Friday, April 5, 13

Slide 216

Slide 216 text

@sandimetz Apr 2013 Use good judgement. Friday, April 5, 13

Slide 217

Slide 217 text

@sandimetz Apr 2013 Insist on simplicity. Friday, April 5, 13

Slide 218

Slide 218 text

@sandimetz Apr 2013 Practice the Tricks. Friday, April 5, 13

Slide 219

Slide 219 text

@sandimetz Apr 2013 And go home every day saying... Friday, April 5, 13

Slide 220

Slide 220 text

@sandimetz Apr 2013 I love my tests. Friday, April 5, 13

Slide 221

Slide 221 text

Thanks Sandi Metz @sandimetz Friday, April 5, 13

Slide 222

Slide 222 text

http://www. ickr.com/photos/phil_shirley/5452562957/ thicket http://www. ickr.com/photos/zebble/6080622/ one padlock http://www. ickr.com/photos/chrisinplymouth/5543281168/ two padlocks http://www. ickr.com/photos/donjohnson395/2915304926/ twins at beach http://www.med.navy.mil/sites/nmcp/Patients/pediatrics/PublishingImages/binocular_boy.gif http://upload.wikimedia.org/wikipedia/commons/e/e1/Ordinary_bicycle02.jpg ordinary bike Magic Hat (82805980) Copyright Mmaxer - Shutterstock.com Dogs in Hats (39263399) Copyright MLoiselle - Fotolia.com Garden (35521385) Copyright Beboy - Fotolia.com Space Walk - Nasa http://grin.hq.nasa.gov/copyright.html Photo Credits Friday, April 5, 13

Slide 223

Slide 223 text

@sandimetz Apr 2013 And one last time... http://poodr.info Friday, April 5, 13

Slide 224

Slide 224 text

@sandimetz Apr 2013 http://poodr.info Friday, April 5, 13

Slide 225

Slide 225 text

@sandimetz Apr 2013 Hot off the press http://poodr.info Friday, April 5, 13

Slide 226

Slide 226 text

Questions? Friday, April 5, 13

Slide 227

Slide 227 text

@sandimetz Apr 2013 http://poodr.info Friday, April 5, 13