Save 37% off PRO during our Black Friday Sale! »

About TDD at Cookpad Summer Intern 2016 day1

About TDD at Cookpad Summer Intern 2016 day1

70e13d9877054026fda46d5a5b53a236?s=128

MOROHASHI Kyosuke

August 12, 2016
Tweet

Transcript

  1. TDD: Test Driven Development 2016-08-12 ΫοΫύουαϚʔΠϯλʔϯ 2016 ٕज़෦ ։ൃج൫ @moro

    ॾڮګհ
  2. Why TDD?

  3. ʮಈ࡞͢Δ͖Ε͍ͳίʔυʯɺϩϯɾδΣϑϦʔ ζͷ͜ͷ؆ܿͳݴ༿͸ɺ5%% ςετۦಈ։ൃ ͷ ໨ඪͰ͋Δɻಈ࡞͢Δ͖Ε͍ͳίʔυ͸ɺ͋ΒΏ Δཧ༝ͰՁ஋͕͋Δɻ ᴷ,FOU#FDL

  4. ಈ࡞͢Δɺ͖Ε͍ͳίʔυ΁ ͖Ε͍ Ԛ͍ (͙͢ʹ͸)ಈ͔ͳ͍ ಈ࡞͢Δ ೋͭͷಓ͕͋Δ t-wada: says

  5. What is TDD?

  6. TDD = Test Driven
 Development

  7. ‣ ·ͣಈ࡞͢ΔίʔυΛॻ͖ɺͦΕΛ͖Ε͍ͳίʔυʹ͢Δɺ ͱ͍͏ΞϓϩʔνΛऔΔιϑτ΢ΣΞͷઃܭɾ։ൃख๏ɻ ಈ࡞͢Δɺ͖Ε͍ͳίʔυ΁ 

  8. ςετۦಈ։ൃ͸ɺϓϩάϥϛϯάதͷෆ҆Λ؅ཧ͢Δख๏ ͩɻෆ҆ͱ͸ʮ͜Ε͸ࠔ೉ͳ໰୊ͳͷͰɺ࠷ॳ͔Β͢΂ͯΛ ݟ௨ͤΔΘ͚Ͱ͸ͳ͍ʯͱ͍͏ਅͬ౰ͳײ֮ͷ͜ͱͩɻ ུ  5%%͸ϓϩάϥϛϯάதͷઃܭ൑அͱϑΟʔυόοΫͷؒʹ ͋ΔΪϟοϓΛೝࣝ͢Δ͜ͱͰ͋ΓɺͦͷΪϟοϓΛίϯτ ϩʔϧ͢Δٕ๏Ͱ΋͋Δɻ ςετۦಈ։ൃೖ໳

  9. ‣ ιϑτ΢ΣΞΛ࣮ߦ͠ɺͦͷ݁ՌΛ֬ೝͯ࣍͠ʹॻ͘ίʔ υΛܾΊΔɻ ‣ ςεςΟϯάϑϨʔϜϫʔΫ͕ͦΕΛࢧԉͯ͘͠ΕΔɻ ‣ ࣮ߦ͢ΔͨΊͷ४උ(σʔληοτΞοϓͳͲ) ‣ ࣮ߦ݁Ռͷࣗಈ֬ೝ ‣

    ܁Γฦ࣮͠ߦͷࢧԉ ϑΟʔυόοΫ 
  10. How to TDD?

  11. How to learn TDD?

  12. कɾഁɾ཭

  13. ‣ Ϩου৽͍͠ίʔυΛॻ͘લʹɺ·ͣ͸ࣦഊ͢ΔϢχοτςετΛॻ͘ɻ৽͠ ͍ίʔυͷҙਤΛςετͰࣔ͢ΜͩɻϨου͸ઃܭʹ͍͔ͭͯͬ͠Γߟ͑Δग़ ൃ఺ͩɻ ‣ άϦʔϯͱʹ͔͘ςετʹ੒ޭ͢ΔίʔυΛॻ͘ɻ࣮૷ͷ࠷ऴܗΛࢥ͍ඳ͚Δ ͳΒҰؾʹ࢓্͛ͯ͠·͑͹͍͍ɻͦ͜·Ͱͷࣗ৴͕ͳ͚Ε͹ɺ·ͣ͸ςετ ʹ੒ޭ͢Δఔ౓ͷ࣮૷͚ͩͰߏΘͳ͍ɻ ‣ ϦϑΝΫλϦϯά࣮૷Λݟ௚͢ɻςετΛ੒ޭͤ͞ΔͨΊʹ΍Β͔ͨ͠࿛᤻ͷ

    ਺ʑΛ͖Ε͍ʹ૟আ͠Α͏ɻॏෳΛऔΓআ͘ɻແବΛল͘ɻҙਤΛ͸͖ͬΓ͞ ͤΔɻग़དྷΔݶΓ໌ྎͰ෼͔Γ΍͍͢ίʔυʹ͠Α͏ɻ ΞδϟΠϧαϜϥΠୈষςετۦಈ։ൃ
  14. 5%%ͷαΠΫϧ 1.࣍ͷ໨ඪΛߟ͑Δ 2.ͦͷ໨ඪΛࣔ͢ςετΛॻ͘ 3.ͦͷςετΛ࣮ߦࣦͯ͠ഊͤ͞Δ 3FE  4.໨తͷίʔυΛॻ͘ 5.Ͱॻ͍ͨςετΛ੒ޭͤ͞Δ (SFFO 

    6.ςετ͕௨Δ··ͰϦϑΝΫλϦϯάΛߦ ͏ 3FGBDUPS  7.̍ʙΛ܁Γฦ͢ t-wada: says
  15. ͖Ε͍ Ԛ͍ (͙͢ʹ͸)ಈ͔ͳ͍ ಈ࡞͢Δ Red Green Refactoring 5%%ͱԫۚͷճస t-wada: says

  16. ͓୊ ࣌ܭΦϒδΣΫτ

  17. ‣ ࣌ɺ෼ɺඵΛ࣋ͭ ‣ ʮ10࣌20෼0ඵʯͳͲɺಛఆͷ࣌ࠁΛද͢ ‣ ೔෇ͱ߹ΘͤΔ͜ͱͰɺಛఆͷ࣌ؒΛࢦ͢ ‣ ࣍ͷ೔࣌ɺલͷ೔࣌Λಋग़Ͱ͖Δ ࣌ܭΦϒδΣΫτͷ࢓༷Λߟ͑Δ 

  18. $ cd # お好きなところ $ mkdir clock # 好きな名前 $

    cd clock $ git init Bootstrap ‣ ৽͘͠ͳʹ͔Λ͢Δͱ͖͸ɺσΟϨΫτϦΛͭͬͯ͘
 ͙͢(JUͰ؅ཧ࢝͠ΊΔश׳Λ͚ͭ·͠ΐ͏ɻ 
  19. $ gem install bundler $ bundle init $ vim Gemfile

    ``` # frozen_string_literal: true # A sample Gemfile source "https://rubygems.org" gem "rspec" ``` $ bundle install $ git add . $ git commit -m 'bundle init' Bootstrap 
  20. $ bundle exec rspec --init $ git add . $

    git commit -m 'rspec --init' Bootstrap 
  21. ‣ ͍·ηοτΞοϓͨ͠RSpec͸
 ͪΌΜͱಈ͍͍ͯΔ͔ͳ? ࣍ͷ໨ඪΛߟ͑Δ 

  22. $ vim spec/clock_spec.rb ``` describe 'Clock' do specify { expect(true).to

    be true } end ``` ``` $ bundle exec rspec spec/clock_spec.rb . Finished in 0.00097 seconds (files took 0.09778 seconds to load) 1 example, 0 failures ``` ࠷ॳͷҰา 
  23. ‣ RSpec͕ͪΌΜͱΠϯετʔϧ
 Ͱ͖ͯɺಈ͍͍ͯΔ ղফͰ͖ͨෆ҆ 

  24. ‣ Ϩου৽͍͠ίʔυΛॻ͘લʹɺ·ͣ͸ࣦഊ͢ΔϢ χοτςετΛॻ͘ɻ৽͍͠ίʔυͷҙਤΛςετ Ͱࣔ͢ΜͩɻϨου͸ઃܭʹ͍͔ͭͯͬ͠Γߟ͑Δ ग़ൃ఺ͩɻ ΞδϟΠϧαϜϥΠୈষςετۦಈ։ൃ

  25. ‣ ʮ࣌ܭΫϥεʯͷΫϥε໊͸Ͳ͏͠Α͏? ‣ Tokei ..? ‣ Clock ? ‣ etc..

    ࣍ͷ໨ඪΛߟ͑Δ 
  26. ``` describe Clock do specify { expect(Clock).to be_an_instance_of(Class) } end

    ``` $ bundle exec rspec spec bundler: failed to load command: rspec 
 (/opt/brew/opt/rbenv/versions/2.3.0/bin/rspec) NameError: uninitialized constant Clock ClockΫϥεΛ࡞Γ͍ͨ → ·ͩͳ͍͔ΒΤϥʔʹͳΔ͸ͣ 
  27. ‣ ະॳظԽͷఆ਺ClockΛࢀরͨ͠Αɺͱ͍͏Τϥʔ ‣ → ·ͩͳ͍͔ΒΤϥʔʹͳΔ͸ͣ
 ظ଴௨Γͷཧ༝Ͱͷςετࣦഊ ClockΫϥεΛ࡞Δ४උ 

  28. ‣ ςετ͕ظ଴௨Γʹࣦഊ͢Δ͜ͱΛ֬ೝ͢Δͷ͸ͱ ͯ΋େࣄɻ ‣ ݟ౰͸ͣΕͷ໰୊Ͱ೰Ή͜ͱ͕͋Δɻ ‣ eg: SyntaxErrorΛݟམͱͨ͠··ClockΫϥεΛ
 ࣮૷ͯ͠΋ద੾ͳϑΟʔυόοΫʹͳΒͳ͍ ظ଴௨Γͷཧ༝Ͱͷςετͷࣦഊ

    
  29. $ mkdir lib $ vim lib/clock.rb ``` class Clock end

    ``` $ bundle exec rspec spec bundler: failed to load command: rspec 
 (/opt/brew/opt/rbenv/versions/2.3.0/bin/rspec) NameError: uninitialized constant Clock ClockΫϥεΛ࡞Δ → ಡΈࠐ·ͳ͍ͱࢀরͰ͖ͳ͍ͷ͸มΘΒͳ͍͸ͣ 
  30. ‣ҙਤͱ߹͍ͬͯΔ? ‣`require`͍ͯ͠ͳ͍͔ΒࢀরͰ͖ͳ͍ ‣→ okΆ͍ ςετࣦഊͷཧ༝Λ֬ೝ͢Δ 

  31. ``` require 'clock' describe Clock do specify { expect(Clock).to be_an_instance_of(Class)

    } end ``` $ bundle exec rspec spec . Finished in 0.00462 seconds (files took 0.09435 seconds to load) 1 example, 0 failures require΋ͨ͠ˠ͜ΜͲͦ͜௨Δ͸ͣ 
  32. ‣ ϓϩμΫτίʔυͱςετίʔυΛɺͦΕͧΕ
 Ͳ͜ʹॻ͚͹Α͍ͷ͔͕Θ͔ͬͨ ‣ ϓϩμΫτίʔυ͸ lib/clock.rb ‣ ςετίʔυ͸ spec/clock_spec.rb ղফͰ͖ͨෆ҆

    
  33. ςετʹಋ͔Ε࣮ͯ૷͢Δɺ ͨΊʹ࢓༷Λཧղ͢Δ

  34. ‣ ࣌ɺ෼ɺඵΛ࣋ͭ ‣ ʮ10࣌20෼0ඵʯͳͲɺಛఆͷ࣌ࠁΛද͢ ‣ ೔෇ͱ߹ΘͤΔ͜ͱͰɺಛఆͷ࣌ؒΛࢦ͢ ‣ ࣍ͷ೔࣌ɺલͷ೔࣌Λಋग़Ͱ͖Δ ࣌ܭΦϒδΣΫτͷ࢓༷Λߟ͑Δ 

  35. ࣌ɺ෼ɺඵΛ࣋ͭ

  36. ‣ ࣌ɺ෼ɺඵΛ༩͑ͯΠϯελϯε࡞Δ͜ͱ͕Ͱ͖Δ ‣ Πϯελϯεੜ੒࣌ʹ༩͑ͨ࣌ɺ෼ɺඵΛɺ
 ͦΕͧΕऔಘͰ͖Δ ‣ ࣌ܭͱͯ͠ෆద੾ͳύϥϝʔλΛ༩͑ͨ৔߹ʹ͸ɺΤϥʔ ͱ͢Δ͜ͱ ࣌ɺ෼ɺඵΛ࣋ͭ 

  37. ‣ 10࣌20෼0ඵͱ͍͏ύϥϝʔλΛ༩͑ͯɺClockΫϥεͷ
 ΠϯελϯεΛੜ੒Ͱ͖Δɻ ‣ ੜ੒ͨ͠Πϯελϯεͷʮ࣌ʯ͸10Ͱ͋Δɻ ΠϯελϯεΛͭͬͯ͘ɺ༩͑ͨ࣌෼ඵΛऔಘͰ͖Δ 

  38. ‣ 10࣌20෼0ඵͱ͍͏ύϥϝʔλΛ༩͑ͯɺClockΫϥεͷΠ ϯελϯεΛੜ੒Ͱ͖Δ ‣ ίϯετϥΫλͰɺ࣌ɺ෼ɺඵΛ༩͑Ε͹Α͍? ‣ ੜ੒ͨ͠Πϯελϯεͷʮ࣌ʯ͸10Ͱ͋Δ ‣ ήολʔ͕͋Ε͹Αͦ͞͏ ‣

    ηολʔ͸? ݟ͍͑ͯΔݶΓ͸ෆཁͦ͏ ࣍ͷ໨ඪΛߟ͑Δ 
  39. --- a/spec/clock_spec.rb +++ b/spec/clock_spec.rb @@ -2,4 +2,10 @@ require 'clock'

    describe Clock do specify { expect(Clock).to be } + + context 'new(10, 20, 00)' do + let(:clock) { Clock.new(10, 20, 0) } + + specify { expect(clock.hour).to eq(10) } + end end ࣮श1: 5෼ ΠϯελϯεΛੜ੒͢Δςετ ‣ ࣮ߦલʹ ͜ͷςετΛ࣮ߦ͢ΔͱͲ͏ͳΓͦ͏͔ɺ༧૝͠ͳ͍͞ɻ ‣ ࣮ߦ͠ɺ݁ՌΛ֬ೝ͠ͳ͍͞ 
  40. $ bundle exec rspec .F Failures: 1) Clock new(10, 20,

    00) Failure/Error: let(:clock) { Clock.new(10, 20, 0) } ArgumentError: wrong number of arguments (given 3, expected 0) # ./spec/clock_spec.rb:7:in `initialize' # ./spec/clock_spec.rb:7:in `new' # ./spec/clock_spec.rb:7:in `block (3 levels) in <top (required)>' # ./spec/clock_spec.rb:9:in `block (3 levels) in <top (required)>' Finished in 0.00186 seconds (files took 0.08364 seconds to load) 2 examples, 1 failure Failed examples: rspec ./spec/clock_spec.rb:9 # Clock new(10, 20, 00) ੜ੒͢ΔςετˠίϯετϥΫλҾ਺͕ҧࣦͬͯഊ͢Δ͸ͣ 
  41. ‣ άϦʔϯͱʹ͔͘ςετʹ੒ޭ͢ΔίʔυΛॻ͘ɻ ࣮૷ͷ࠷ऴܗΛࢥ͍ඳ͚ΔͳΒҰؾʹ࢓্͛ͯ͠· ͑͹͍͍ɻͦ͜·Ͱͷࣗ৴͕ͳ͚Ε͹ɺ·ͣ͸ςε τʹ੒ޭ͢Δఔ౓ͷ࣮૷͚ͩͰߏΘͳ͍ɻ ΞδϟΠϧαϜϥΠୈষςετۦಈ։ൃ

  42. ``` --- a/lib/clock.rb +++ b/lib/clock.rb @@ -1,2 +1,8 @@ class

    Clock + def initialize(*args) + end + + def hour + 10 + end end ``` Fake it (Ծ࣮૷) 
  43. ‣ ΦϒδΣΫτͷར༻ऀͱͯ͠ظ଴͢Δ
 ৼΔ෣͍ΛɺςετίʔυͰදݱͰ͖ͨ ‣ ҙਤͨ͠ͱ͓ΓʹΤϥʔʹͳͬͨ = ςετίʔυ͸ؒҧ͍ͬͯͳͦ͏ ‣ ଈ஋Λฦͤ͹௨ͬͨ =

    ·͢·͢Αͦ͞͏ ‣ ͜ΕΛ*ͪΌΜͱ*௨ͦ͏ ղফͰ͖ͨෆ҆ 
  44. --- a/spec/clock_spec.rb +++ b/spec/clock_spec.rb @@ -8,4 +8,10 @@ describe Clock

    do specify { expect(clock.hour).to eq(10) } end + + context 'new(12, 30, 00)' do + let(:clock) { Clock.new(12, 30, 0) } + + specify { expect(clock.hour).to eq(12) } + end end ࡾ֯ଌྔ (Triangulate) ‣ ࣦഊ͢ΔςετΛ௥Ճ͢Δ ‣ ͷςετ͕௨Δ··ɺͷςετΛ௨͢Α͏ͳɺ࠷௿ݶͷ࣮૷Λ ࡞Δ 
  45. ‣ άϦʔϯͱʹ͔͘ςετʹ੒ޭ͢ΔίʔυΛॻ͘ɻ ࣮૷ͷ࠷ऴܗΛࢥ͍ඳ͚ΔͳΒҰؾʹ࢓্͛ͯ͠· ͑͹͍͍ɻͦ͜·Ͱͷࣗ৴͕ͳ͚Ε͹ɺ·ͣ͸ςε τʹ੒ޭ͢Δఔ౓ͷ࣮૷͚ͩͰߏΘͳ͍ɻ ΞδϟΠϧαϜϥΠୈষςετۦಈ։ൃ

  46. --- a/lib/clock.rb +++ b/lib/clock.rb @@ -1,8 +1,9 @@ class Clock

    - def initialize(*args) + def initialize(hour, *) + @hour = hour end def hour - 10 + @hour end end 12:30ͷςετΛ௨͢Α͏ͳɺ࠷௿ݶͷ࣮૷ 
  47. ‣ [done] 10࣌20෼0ඵͱ͍͏ύϥϝʔλΛ༩͑ͯɺ ClockΫϥεͷΠϯελϯεΛੜ੒Ͱ͖Δ ‣ [done] ੜ੒ͨ͠Πϯελϯεͷʮ࣌ʯ͸10Ͱ͋Δ ղফͰ͖ͨෆ҆ 

  48. ʮಈ࡞͢Δ͖Ε͍ͳίʔυʯɺϩϯɾδΣϑϦʔ ζͷ͜ͷ؆ܿͳݴ༿͸ɺ5%% ςετۦಈ։ൃ ͷ ໨ඪͰ͋Δɻಈ࡞͢Δ͖Ε͍ͳίʔυ͸ɺ͋ΒΏ Δཧ༝ͰՁ஋͕͋Δɻ ᴷ,FOU#FDL

  49. ‣ ϦϑΝΫλϦϯά࣮૷Λݟ௚͢ɻςετΛ੒ޭͤ͞ΔͨΊ ʹ΍Β͔ͨ͠࿛᤻ͷ਺ʑΛ͖Ε͍ʹ૟আ͠Α͏ɻॏෳΛऔ Γআ͘ɻແବΛল͘ɻҙਤΛ͸͖ͬΓͤ͞Δɻग़དྷΔݶΓ ໌ྎͰ෼͔Γ΍͍͢ίʔυʹ͠Α͏ɻ ΞδϟΠϧαϜϥΠୈষςετۦಈ։ൃ

  50. ‣ ςετ͕௨Δ··ͰϦϑΝΫλϦϯάΛߦ͏ ‣ ʮάϦʔϯ to άϦʔϯʯͱ͍͏ݴ͍ճ͠ {ಈ࡞͢Δ && ͖Ε͍ͳ}ίʔυΛ໨ࢦͯ͠ 

  51. --- a/lib/clock.rb +++ b/lib/clock.rb @@ -1,9 +1,8 @@ class Clock

    + attr_reader :hour + def initialize(hour, *) @hour = hour end - def hour - @hour - end end {ಈ࡞͢Δ && ͖Ε͍ͳ}ίʔυΛ໨ࢦͯ͠ 
  52. --- a/spec/clock_spec.rb +++ b/spec/clock_spec.rb @@ -1,8 +1,6 @@ require 'clock'

    describe Clock do - specify { expect(Clock).to be_an_instance_of(Class) } - context 'new(10, 20, 00)' do let(:clock) { Clock.new(10, 20, 0) } ςετίʔυ΋ϦϑΝΫλϦϯά͢Δ 
  53. ‣ ςετίʔυ͸ϕλʹॻ͔͘ɺDRY-nessΛอ͔ͭ ͸ٞ࿦͕෼͔Ε͍͕ͯͨɺ͍·ͷ਼੎͸ɺςετίʔ υ΋ʮίʔυʯͳͷͰͪΌΜͱॻ͘ͱ͍͏΋ͷ ‣ ॏෳ΍แؚʹΑΔՁ஋ͷ௿͍ςετίʔυ͸מΓࠐ ΜͰ͍͘ ‣ ͨͩמΓ͗ͯ͢ɺ֊ஈ͕ٸʹͳΓ͗͢Δͷ΋ྑ͘ͳ͍ ΩϨΠͳίʔυ

    
  54. ‣ 10࣌20෼0ඵͱ͍͏ύϥϝʔλΛ༩͑ͯɺClockΫϥεͷΠ ϯελϯεΛੜ੒Ͱ͖Δ ‣ ίϯετϥΫλͰɺ࣌ɺ෼ɺඵΛ༩͑Ε͹Α͍? ‣ ੜ੒ͨ͠Πϯελϯεͷʮ࣌ʯ͸10Ͱ͋Δ ‣ ෼͸20Ͱ͋Δɻ ‣

    ඵ͸0Ͱ͋Δɻ ࣍ͷ໨ඪΛߟ͑Δ 
  55. --- a/spec/clock_spec.rb +++ b/spec/clock_spec.rb @@ -5,6 +5,8 @@ describe Clock

    do let(:clock) { Clock.new(10, 20, 0) } specify { expect(clock.hour).to eq(10) } + specify { expect(clock.min).to eq(20) } + specify { expect(clock.sec).to eq(0) } end context 'new(12, 30, 00)' do ෼ͱඵʹର͢Δςετˠ#hour, #minϝιου͕ͳ͍ͷͰࣦഊ͢Δ͸ͣ 
  56. ‣ άϦʔϯͱʹ͔͘ςετʹ੒ޭ͢ΔίʔυΛॻ͘ɻ ࣮૷ͷ࠷ऴܗΛࢥ͍ඳ͚ΔͳΒҰؾʹ࢓্͛ͯ͠· ͑͹͍͍ɻͦ͜·Ͱͷࣗ৴͕ͳ͚Ε͹ɺ·ͣ͸ςε τʹ੒ޭ͢Δఔ౓ͷ࣮૷͚ͩͰߏΘͳ͍ɻ ΞδϟΠϧαϜϥΠୈষςετۦಈ։ൃ

  57. --- a/lib/clock.rb +++ b/lib/clock.rb @@ -1,8 +1,10 @@ class Clock

    - attr_reader :hour + attr_reader :hour, :min, :sec - def initialize(hour, *) + def initialize(hour, min, sec) @hour = hour + @min = min + @sec = sec end end ໌നͳ࣮૷ (Obvious Implementation) 
  58. ‣ [done] 10࣌20෼0ඵͱ͍͏ύϥϝʔλΛ༩͑ͯɺClockΫ ϥεͷΠϯελϯεΛੜ੒Ͱ͖Δ ‣ [done] ੜ੒ͨ͠Πϯελϯεͷʮ࣌ʯ͸10Ͱ͋Δ ‣ [done] ʮ෼ʯ͸20Ͱ͋Γɺʮඵʯ͸0Ͱ͋Δ

    ղফͰ͖ͨෆ҆ 
  59. ‣ ࣌ɺ෼ɺඵΛ༩͑ͯΠϯελϯε࡞Δ͜ͱ͕Ͱ͖Δ ‣ Πϯελϯεੜ੒࣌ʹ༩͑ͨ࣌ɺ෼ɺඵΛɺ
 ͦΕͧΕऔಘͰ͖Δ ‣ ࣌ܭͱͯ͠ෆద੾ͳύϥϝʔλΛ༩͑ͨ৔߹ʹ͸ɺΤϥʔ ͱ͢Δ͜ͱ ࣌ɺ෼ɺඵΛ࣋ͭ 

  60. expect { ブロック }.to raise_error(例外クラス[, メッセージ]) ࢀߟ: RSpecͷྫ֎Ϛονϟʔ ‣ ϒϩοΫΛධՁͨ͠ࡍʹɺࢦఆͨ͠ྫ֎͕ൃੜ͢Δ͜ͱΛݕূ͢Δ

    
  61. --- a/spec/clock_spec.rb +++ b/spec/clock_spec.rb @@ -14,4 +14,9 @@ describe Clock

    do specify { expect(clock.hour).to eq(12) } end + + context 'new(12, 30, 60)' do + specify { expect { Clock.new(12, 30, 60) }.to raise_error(ArgumentError) } + end + end ࣮श2: 15෼ ҎԼͷςετΛ࡞੒͠ɺࣦഊΛ֬ೝͨ͠͏͑Ͱ࣮૷͠ͳ͍͞ 
  62. ‣ ೖྗ͕ෛ஋Ͱ͋ͬͨέʔεͷςετΛ௥Ճ͠ɺ
 ࣮૷͠ͳ͍͞ɻ ‣ ςετΛ௨͢࠷௿ݶͷ࣮૷ͰάϦʔϯʹ͢Δ ‣ ͦͷޙɺϦϑΝΫλϦϯά͢Δ ࣮श3: 20෼ 

  63. --- a/lib/clock.rb +++ b/lib/clock.rb @@ -2,6 +2,10 @@ class Clock

    attr_reader :hour, :min, :sec def initialize(hour, min, sec) + if (hour >= 24) || (min >= 60) || (sec >= 60) + raise ArgumentError + end + @hour = hour @min = min @sec = sec ਖ਼ͷ਺ͷൣғ֎͚ͩͰΤϥʔʹͳΔ 
  64. --- a/spec/clock_spec.rb +++ b/spec/clock_spec.rb @@ -19,4 +19,7 @@ describe Clock

    do specify { expect { Clock.new(12, 30, 60) }.to raise_error(ArgumentError) } end + context 'new(-12, 30, 59)' do + specify do + expect { Clock.new(-12, 30, 59) }. + to raise_error(ArgumentError) + end + end end ࣮श3ͷςετΛ࢖ͬͯࡾ֯ଌྔ͢Δ 
  65. --- a/lib/clock.rb +++ b/lib/clock.rb @@ -2,7 +2,7 @@ class Clock

    attr_reader :hour, :min, :sec def initialize(hour, min, sec) - if (hour >= 24) || (min >= 60) || (sec >= 60) + if (hour < 0 || hour >= 24) || (min < 0 || min >= 60) 
 || (sec < 0 || sec >= 60) raise ArgumentError end ࣮श2͕άϦʔϯͳ··ɺ࣮श3͕௨Δ࠷௿ݶͷίʔυ 
  66. --- a/lib/clock.rb +++ b/lib/clock.rb @@ -2,13 +2,19 @@ class Clock

    attr_reader :hour, :min, :sec def initialize(hour, min, sec) - if (hour < 0 || hour >= 24) || (min < 0 || min >= 60) || (sec < 0 || sec >= 60) + @hour = verify_valid_clock_input(hour, 24) + @min = verify_valid_clock_input(min, 60) + @sec = verify_valid_clock_input(sec, 60) + end + + private + + def verify_valid_clock_input(val, max) + if (0...max).include?(val) + val + else raise ArgumentError end - - @hour = hour - @min = min - @sec = sec end ϦϑΝΫλϦϯάྫ 
  67. ೔෇ͱ߹ΘͤΔ͜ͱͰɺ ಛఆͷ࣌ؒΛࢦ͢

  68. ‣ ࣍ͷ೔࣌ɺલͷ೔࣌Λಋग़Ͱ͖Δ ‣ ͖ΐ͏ͷʮ10:20ʯ͸͍͔ͭ ‣ 2016-08-12T10:20:00+JST ೔෇ͱ߹ΘͤΔ͜ͱͰɺಛఆͷ࣌ؒΛࢦ͢ 

  69. --- a/spec/clock_spec.rb +++ b/spec/clock_spec.rb @@ -22,4 +22,11 @@ describe Clock

    do context 'new(-12, 30, 59)' do specify { expect { Clock.new(-12, 30, 59) }.to raise_error(ArgumentError) } end + + describe '#to_time' do + let(:clock) { Clock.new(10, 20, 0) } + let(:time) { clock.to_time(Date.today) } + + specify { + expect(time.iso8601).to eq ‘2016-08-12T10:20:00+09:00' + } + end end APIΛߟ͑Δ #to_time(<#Date>)͕ૉ௚ͦ͏ˠ#to_timeͷ࣮૷ͳ͍ 
  70. ‣ લϖʔδͷςετʹର͢Δ࣮૷ίʔυΛॻ͖ͳ͍͞ɻ ‣ Ծ࣮૷ɺࡾ֯ଌྔɺϦϑΝΫλϦϯάΛɺͦΕͧΕί ϛοτΛ෼͚࣮ͯࢪ͠ͳ͍͞ ‣ [Advanced] ͜ͷςετίʔυʹ͸େ͖ͳ໰୊͕͋Γ ·͢ɻվળ͠ͳ͍͞ɻ ࣮श4:

    15෼ 
  71. --- a/lib/clock.rb +++ b/lib/clock.rb @@ -7,6 +7,10 @@ class Clock

    @sec = varify_valid_clock_input(sec, 60) end + def to_time(date) + Time.local(2016, 6, 22, 10, 20) + end + private def varify_valid_clock_input(val, max) ճ౴ྫ: Ծ࣮૷ 
  72. ‣ ೔෇ͱ߹ΘͤΔ͜ͱͰɺಛఆͷ࣌ؒΛࢦ͢ ‣ ຊ೔ͷɺ10:20Λදݱ͢Δ ‣ 8/1ͷɺ10:20Λදݱ͢Δ ࡾ֯ଌྔ 

  73. ‣ ࣍ͷ೔࣌ɺલͷ೔࣌Λಋग़Ͱ͖Δ ‣ ͖ΐ͏ͷ 10:20 ͸ 2016-08-12T10:20:00 JST ‣ 8/1ͷ

    10:20 ͸ 2016-08-01T10:20:00 JST ࡾ֯ଌྔͷexampleΛ۩ମԽ 
  74. --- a/spec/clock_spec.rb +++ b/spec/clock_spec.rb @@ -25,8 +25,16 @@ describe Clock

    do describe '#to_time' do let(:clock) { Clock.new(10, 20, 0) } - let(:time) { clock.to_time(Date.today) } + context '(Date.today)' do + let(:time) { clock.to_time(Date.today) } - specify { expect(time.iso8601).to eq '2016-06-22T10:20:00+09:00' } + specify { expect(time.iso8601).to eq '2016-06-22T10:20:00+09:00' } + end + + context '(2016-08-01)' do + let(:time) { clock.to_time(Date.new(2016, 8, 1)) } + + specify { expect(time.iso8601).to eq '2016-08-01T10:20:00+09:00' } + end end end ࡾ֯ଌྔ 
  75. --- a/lib/clock.rb +++ b/lib/clock.rb @@ -8,7 +8,7 @@ class Clock

    end def to_time(date) - Time.local(2016, 6, 22, 10, 20) + Time.local(date.year, date.month, date.day, @hour, @min, @sec) end private ࡾ֯ଌྔ 
  76. --- a/spec/clock_spec.rb +++ b/spec/clock_spec.rb @@ -26,7 +26,7 @@ describe Clock

    do describe '#to_time' do let(:clock) { Clock.new(10, 20, 0) } context '(Date.today)' do - let(:time) { clock.to_time(Date.today) } + let(:time) { clock.to_time(Date.new(2016, 6, 22)) } specify { expect(time.iso8601).to eq '2016-06-22T10:20:00+09:0 end ςετίʔυͷվળ: ֎෦ґଘ஋Λແ͘͢ 
  77. ‣ 5%%͸ϓϩάϥϛϯάதͷઃܭ൑அͱϑΟʔυόοΫͷؒ ʹ͋ΔΪϟοϓΛೝࣝ͢Δ͜ͱͰ͋ΓɺͦͷΪϟοϓΛί ϯτϩʔϧ͢Δٕ๏Ͱ΋͋Δɻ ςετۦಈ։ൃೖ໳  ‣ ద੾ͳϑΟʔυόοΫΛಘଓ͚ΔΔͨΊʹେࣄͳ΋ͷ ‣ Repeatable:

    ͍࣮ͭߦͯ͠΋ಉ݁͡ՌʹͳΔ͜ͱ ‣ Independent: ଞͷςετέʔεʹґଘ͠ͳ͍͜ͱ Α͍ςετίʔυͱ͸ 
  78. ࣍ͷ೔࣌ɺલͷ೔࣌Λಋ ग़Ͱ͖Δ

  79. ‣ 2016-08-11T12:30+JST ͷ࣍ͷ 10:20 Λಋग़Ͱ͖Δ ࣍ͷ೔࣌ɺલͷ೔࣌Λಋग़Ͱ͖Δ 

  80. --- a/spec/clock_spec.rb +++ b/spec/clock_spec.rb @@ -1,12 +1,12 @@ require 'clock'

    describe Clock do - context 'new(10, 20, 00)' do - let(:clock) { Clock.new(10, 20, 0) } + let(:clock_10_20) { Clock.new(10, 20, 0) } - specify { expect(clock.hour).to eq 10 } - specify { expect(clock.min).to eq 20 } - specify { expect(clock.sec).to eq 0 } + context 'new(10, 20, 00)' do + specify { expect(clock_10_20.hour).to eq 10 } + specify { expect(clock_10_20.min).to eq 20 } + specify { expect(clock_10_20.sec).to eq 0 } end context 'new(12, 30, 00)' do @@ -24,17 +24,20 @@ describe Clock do end describe '#to_time' do - let(:clock) { Clock.new(10, 20, 0) } context '(Date.today)' do - let(:time) { clock.to_time(Date.new(2016, 6, 22)) } + let(:time) { clock_10_20.to_time(Date.new(2016, 6, 22)) } specify { expect(time.iso8601).to eq '2016-06-22T10:20:00+09:00' } end context '(2016-08-01)' do - let(:time) { clock.to_time(Date.new(2016, 8, 1)) } + let(:time) { clock_10_20.to_time(Date.new(2016, 8, 1)) } specify { expect(time.iso8601).to eq '2016-08-01T10:20:00+09:00' } end end ͷɺલʹRefactoring: 
  81. describe Clock do - context 'new(10, 20, 00)' do -

    let(:clock) { Clock.new(10, 20, 0) } + let(:clock_10_20) { Clock.new(10, 20, 0) } - specify { expect(clock.hour).to eq 10 } - specify { expect(clock.min).to eq 20 } - specify { expect(clock.sec).to eq 0 } + context 'new(10, 20, 00)' do + specify { expect(clock_10_20.hour).to eq 10 } + specify { expect(clock_10_20.min).to eq 20 } + specify { expect(clock_10_20.sec).to eq 0 } Refactoring: ‣ `Clock.new(10, 20, 0)`͕සग़͢ΔͷͰɺ໊લΛ͚ͭͯɺ·ͱΊΔ ‣ ςετίʔυ಺ͷ৘ใྔ͕૿͑Δ ‣ νϟϯΫԽ͢Δ͜ͱʹΑΓɺϓϩάϥϚ͕֮͑Δ͜ͱ͕ݮΔ 
  82. ‣ 2016-08-11T12:30+JST ͷ࣍ͷ 10:20 Λಋग़Ͱ͖Δ ࣍ͷ೔࣌ɺલͷ೔࣌Λಋग़Ͱ͖Δ 

  83. --- a/spec/clock_spec.rb +++ b/spec/clock_spec.rb @@ -37,7 +37,9 @@ describe Clock

    do end end + describe '#next_time(time)' do + let(:time) { Time.local(2016, 8, 11, 12, 30) } + specify { + expect(clock_10_20.next_time(time).iso8601).to \ + eq ‘2016-08-12T10:20:00+09:00' + } end end ·ͣ͸RED→ #next_time()͕ͳ͍͔ΒNME 
  84. ‣ ϓϩμΫτίʔυΛ࣮૷͠ͳ͍͞ ‣ Ծ࣮૷ʙࡾ֯ଌྔ͚ͩͰͳ͘ɺ໌നͳ࣮૷͔Β
 ࢝Ίͯ΋Α͍ ‣ ඞཁʹԠͯ͡ϦϑΝΫλϦϯάΛ࣮ࢪ͢Δ͜ͱ ࣮श෼ 

  85. ‣ 2016-08-11T12:30+JST ͷલͷ 10:20 Λಋग़Ͱ͖Δ ࣍ͷ೔࣌ɺલͷ೔࣌Λಋग़Ͱ͖Δ 

  86. ‣ લͷ೔࣌Λಋग़͢ΔϝιουΛ࣮૷͠ͳ͍͞ ‣ ࣦഊ͢ΔςετΛॻ͖ɺ࠷খݶͷ࣮૷Λ࡞੒͠ɺϦϑΝΫ λϦϯά͢Δ͜ͱ ‣ ಉ༷ʹɺ2016-08-11T09:30+JST ͷલͷ 10:20 Λಋग़͢ΔςετΛ௥Ճ͠ɺ࣮૷͢Δ͜ͱ

    ࣮श6: 20෼ 
  87. ௥Ճ࢓༷

  88. ಛఆ༵೔ͷ࣍ͷ೔࣌ɺ
 લͷ೔࣌Λಋग़Ͱ͖Δ

  89. ‣ 2016-08-12T12:30+JST ͷ࣍ʹདྷΔ
 ਫ༵೔ͷ10:20Λಋग़͠ͳ͍͞ ಛఆ༵೔ͷ࣍ͷ೔࣌ɺલͷ೔࣌Λಋग़Ͱ͖Δ 

  90. ‣ ࣦഊ͢ΔςετΛॻ͖ɺ࠷খݶͷ࣮૷Λ࡞੒͠ɺϦϑΝΫ λϦϯά͢Δ͜ͱ ‣ طଘͷৼΔ෣͍Λมߋ͢Δ৔߹ɺ৽ن௥Ճͨ͠ςετ͸
 ϖϯσΟϯά͠ɺطଘͷมߋ͚ͩΛߦ͏͜ͱ ‣ ಉ༷ʹʮલͷX༵೔ʯͷ۩ମྫΛݕ౼͠ɺςετͱͯ͠
 ॻ͖ى͜͠ɺ࣮૷͠ɺϦϑΝΫλϦϯά͠ͳ͍͞ ࣮श෼

    
  91. ͓ർΕ͞·Ͱͨ͠