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

The Magic Tricks of Testing - Sandi Metz

spreeconf
May 21, 2013
260

The Magic Tricks of Testing - Sandi Metz

spreeconf

May 21, 2013
Tweet

Transcript

  1. @sandimetz May 2013 Sent to self Incoming Outgoing Query Command

    Object Under Test Saturday, May 25, 2013
  2. @sandimetz May 2013 Query: Return something/ change nothing Command: Return

    nothing/ change something Type Saturday, May 25, 2013
  3. @sandimetz May 2013 Query: Return something/ change nothing Command: Return

    nothing/ change something Type Saturday, May 25, 2013
  4. @sandimetz May 2013 Query: Return something/ change nothing Command: Return

    nothing/ change something Type Saturday, May 25, 2013
  5. Query Command Message Origin x Type Outgoing Incoming Type Origin

    @sandimetz May 2013 Message Sent to Self Saturday, May 25, 2013
  6. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    Incoming Query Messages Incoming Saturday, May 25, 2013
  7. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    Incoming Wheel diameter Saturday, May 25, 2013
  8. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    class  Wheel    attr_reader  :rim,  :tire    def  initialize(rim,  tire)    #  ...    end    def  diameter        rim  +  (tire  *  2)    end    #  ... Saturday, May 25, 2013
  9. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    class  Wheel    attr_reader  :rim,  :tire    def  initialize(rim,  tire)    #  ...    end    def  diameter        rim  +  (tire  *  2)    end    #  ... Saturday, May 25, 2013
  10. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    class  Wheel    attr_reader  :rim,  :tire    def  initialize(rim,  tire)    #  ...    end    def  diameter        rim  +  (tire  *  2)    end    #  ... Saturday, May 25, 2013
  11. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  12. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  13. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  14. @sandimetz May 2013 Test incoming query messages by making assertions

    about what they send back Rule Saturday, May 25, 2013
  15. Query Command The Unit Testing Minimalist Incoming Type @sandimetz May

    2013 Message Sent to Self Outgoing Origin Saturday, May 25, 2013
  16. Query Command Assert result The Unit Testing Minimalist Incoming Type

    @sandimetz May 2013 Message Sent to Self Outgoing Origin Saturday, May 25, 2013
  17. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    Incoming Another Incoming Query Message Saturday, May 25, 2013
  18. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    Gear gear_inches Incoming Saturday, May 25, 2013
  19. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  20. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  21. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  22. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  23. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  24. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  25. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  26. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  27. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    Incoming Command Messages Incoming Saturday, May 25, 2013
  28. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    Gear set_cog Incoming Saturday, May 25, 2013
  29. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  30. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  31. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  32. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  33. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  34. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  35. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  36. @sandimetz May 2013 Test incoming command messages by making assertions

    about direct public side effects Rule Saturday, May 25, 2013
  37. Query Command Assert result The Unit Testing Minimalist Incoming Type

    @sandimetz May 2013 Message Sent to Self Outgoing Origin Saturday, May 25, 2013
  38. Query Command Assert result The Unit Testing Minimalist Incoming Type

    @sandimetz May 2013 Message Assert direct public side effects Sent to Self Outgoing Origin Saturday, May 25, 2013
  39. @sandimetz May 2013 Receiver of incoming message has sole responsibility

    for asserting the result direct public side effects DRY It Out Saturday, May 25, 2013
  40. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    Part 2 to Self Incoming Saturday, May 25, 2013
  41. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    Messages Sent to Self to Self Incoming Saturday, May 25, 2013
  42. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    class  Gear      #  ...    def  gear_inches        ratio  *  wheel.diameter    end      private    def  ratio        chainring  /  cog.to_f    end      #  ... end Saturday, May 25, 2013
  43. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    Anti-Pattern to Self Incoming Saturday, May 25, 2013
  44. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  45. @sandimetz GoGaRuCo 2012 @sandimetz May 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? Saturday, May 25, 2013
  46. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  47. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  48. @sandimetz GoGaRuCo 2012 @sandimetz May 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: #gear_inches test is proof enough Saturday, May 25, 2013
  49. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    Anti-Pattern to Self Incoming Saturday, May 25, 2013
  50. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    class  Gear      #  ...    def  gear_inches        ratio  *  wheel.diameter    end      private    def  ratio        chainring  /  cog.to_f    end      #  ... end Saturday, May 25, 2013
  51. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    class  Gear      #  ...    def  gear_inches        ratio  *  wheel.diameter    end      private    def  ratio        chainring  /  cog.to_f    end      #  ... end Saturday, May 25, 2013
  52. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    class  Gear      #  ...    def  gear_inches        ratio  *  wheel.diameter    end      private    def  ratio        chainring  /  cog.to_f    end      #  ... end Saturday, May 25, 2013
  53. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  54. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  55. @sandimetz GoGaRuCo 2012 @sandimetz May 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? Saturday, May 25, 2013
  56. @sandimetz GoGaRuCo 2012 @sandimetz May 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 with every change Saturday, May 25, 2013
  57. @sandimetz May 2013 Do not test private methods Do not

    make assertions about their result Rule Saturday, May 25, 2013
  58. @sandimetz May 2013 Do not test private methods Do not

    make assertions about their result Do not expect to send them Rule Saturday, May 25, 2013
  59. @sandimetz May 2013 Break rule if it saves $$$ during

    development Caveat Saturday, May 25, 2013
  60. Query Command Assert result Assert direct public side-effects The Unit

    Testing Minimalist Incoming Type @sandimetz May 2013 Message Sent to Self Outgoing Origin Saturday, May 25, 2013
  61. Query Command Assert result Assert direct public side-effects The Unit

    Testing Minimalist Incoming Type @sandimetz May 2013 Ignore Message Sent to Self Outgoing Origin Saturday, May 25, 2013
  62. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    Part 3 to Self Incoming Outgoing Saturday, May 25, 2013
  63. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    Outgoing Query Messages to Self Incoming Outgoing Saturday, May 25, 2013
  64. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    Spoiler Alert Same rules as ‘Sent to Self’ to Self Incoming Outgoing Saturday, May 25, 2013
  65. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    diameter Gear gear_inches Wheel to Self Incoming Outgoing Saturday, May 25, 2013
  66. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Incoming Outgoing

    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 Saturday, May 25, 2013
  67. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Incoming Outgoing

    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 Saturday, May 25, 2013
  68. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    Gear Gear Outgoing Wheel diameter Incoming Query gear_inches to Self Incoming Outgoing Saturday, May 25, 2013
  69. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    Anti-Pattern to Self Incoming to Self Incoming Outgoing Saturday, May 25, 2013
  70. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  71. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  72. @sandimetz GoGaRuCo 2012 @sandimetz May 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? Saturday, May 25, 2013
  73. @sandimetz GoGaRuCo 2012 @sandimetz May 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: Duplicates Wheel’s tests Saturday, May 25, 2013
  74. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    Anti-Pattern to Self Incoming to Self Incoming Outgoing Saturday, May 25, 2013
  75. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  76. @sandimetz GoGaRuCo 2012 @sandimetz May 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? Saturday, May 25, 2013
  77. @sandimetz GoGaRuCo 2012 @sandimetz May 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 costs but not bene ts Saturday, May 25, 2013
  78. @sandimetz May 2013 Rule Do not test outgoing query messages

    Do not make assertions about their result Saturday, May 25, 2013
  79. @sandimetz May 2013 Rule Do not test outgoing query messages

    Do not make assertions about their result Do not expect to send them Saturday, May 25, 2013
  80. @sandimetz May 2013 If a message has no visible side-effects

    the sender should not test it Saturday, May 25, 2013
  81. Query Command Assert result Assert direct public side effects The

    Unit Testing Minimalist Incoming Type @sandimetz May 2013 Message Ignore Sent to Self Outgoing Origin Saturday, May 25, 2013
  82. Query Command Assert result Assert direct public side effects The

    Unit Testing Minimalist Incoming Type @sandimetz May 2013 Message Ignore Sent to Self Outgoing Origin Saturday, May 25, 2013
  83. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    Outgoing Command Messages to Self Incoming Outgoing Saturday, May 25, 2013
  84. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    diameter gear_inches set_cog changed Wheel Gear Obs to Self Incoming Outgoing Saturday, May 25, 2013
  85. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  86. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  87. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  88. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Incoming Outgoing

    class  Gear      #  ...    def  set_cog(new_cog)        @cog  =  new_cog    end end Saturday, May 25, 2013
  89. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Incoming Outgoing

    class  Gear      #  ...    def  set_cog(new_cog)        @cog  =  new_cog    end end Saturday, May 25, 2013
  90. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  91. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  92. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    Gear set_cog changed Obs to Self Incoming Outgoing Saturday, May 25, 2013
  93. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    Gear set_cog changed Obs Side Effects to Self Incoming Outgoing Saturday, May 25, 2013
  94. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    changed Gear set_cog Obs Incoming Command Side Effects to Self Incoming Outgoing Saturday, May 25, 2013
  95. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    Gear Outgoing set_cog Obs changed Incoming Command Side Effects to Self Incoming Outgoing Saturday, May 25, 2013
  96. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  97. @sandimetz GoGaRuCo 2012 @sandimetz May 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 This message MUST get sent Saturday, May 25, 2013
  98. @sandimetz GoGaRuCo 2012 @sandimetz May 2013 to Self Outgoing Incoming

    Anti-Pattern to Self Incoming Outgoing Saturday, May 25, 2013
  99. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  100. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  101. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  102. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  103. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  104. @sandimetz GoGaRuCo 2012 @sandimetz May 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? Saturday, May 25, 2013
  105. @sandimetz GoGaRuCo 2012 @sandimetz May 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? Saturday, May 25, 2013
  106. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  107. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  108. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  109. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  110. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  111. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  112. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  113. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  114. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Saturday, May 25, 2013
  115. @sandimetz May 2013 Break rule if side effects are stable

    and cheap Caveat Saturday, May 25, 2013
  116. Query Command Assert result Assert direct public side effects Ignore

    The Unit Testing Minimalist Incoming Type @sandimetz May 2013 Message Ignore Sent to Self Outgoing Origin Saturday, May 25, 2013
  117. Query Command Assert result Assert direct public side effects Ignore

    Expect to send The Unit Testing Minimalist Incoming Type @sandimetz May 2013 Message Ignore Sent to Self Outgoing Origin Saturday, May 25, 2013
  118. @sandimetz GoGaRuCo 2012 @sandimetz May 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 Observer stops implementing #changed? Saturday, May 25, 2013
  119. @sandimetz May 2013 Honor the contract Ensure test doubles stay

    in sync with the API Rule Saturday, May 25, 2013
  120. @sandimetz May 2013 Automagically  minitest: requires that stubbed methods exist

     rspec: #should_receive(:msg).and_call_original  https://github.com/psyho/bogus  https://github.com/benmoss/quacky  https://github.com/xaviershay/rspec-­‐fire  https://github.com/cfcosta/minitest-­‐firemock Saturday, May 25, 2013
  121. Query Command Assert result Assert direct public side effects Ignore

    Expect to send Incoming Type @sandimetz May 2013 Message Ignore Sent to Self Outgoing Origin Saturday, May 25, 2013
  122. 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 Saturday, May 25, 2013