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

『アジャイルソフトウェア開発の奥義』から学ぶテスト駆動開発

Avatar for yonetty yonetty
February 16, 2021

 『アジャイルソフトウェア開発の奥義』から学ぶテスト駆動開発

社内の勉強会で使用した資料。

Avatar for yonetty

yonetty

February 16, 2021
Tweet

More Decks by yonetty

Other Decks in Programming

Transcript

  1. ςετϑΝʔετ͕ͩ ςετۦಈ։ൃͰͳ͍ྫ  ઌʹ͢΂ͯͷςετίʔυΛॻ͘ class FooTest { def testFoo1() {

    } def testFoo2() { } def testFoo3() { } def testFoo4() { } … } class Foo { // ͜Ε͔Β࣮૷ }
  2. w ςετ͢΂͖5ISPXͷৼ෣͍͕ݟ͍ͩͤͣɺ 'SBNFͷςετ͔Βॻ͖࢝Ίͨɻ w ࠷΋୯७ͳςετέʔε͔Β࢝Ίɺ࠷΋୯७ͳ ࣮૷ͰςετΛ௨͢ɻ ࠷ॳͷςετέʔε public class Frame

    { public int getScore() { return 0 ; } } class FrameSpec extends Specification { def "ະ౤ٿͷͱ͖ͷείΞ͸0Ͱ͋Δ"() { given : def f = new Frame( ) when : def score = f.getScore( ) then : score == 0 } } ˞ॻ੶Ͱ͸+6OJUΛ࢖͍ͬͯΔ ͕ɺຊࢿྉͰ͸4QPDLΛ࢖༻
  3. w ࣍ʹ୯७ͳςετέʔεΛ௥Ճ͠ɺςετ͕௨ ΔΑ͏ʹίʔυΛमਖ਼͢Δ ࣍ͷςετέʔε class FrameSpec extends Specification { ..

    def "1ճ౤ٿͨ͠ͱ͖ʹείΞ͕ਖ਼͍͠"() { given : def f = new Frame( ) when : f.add(5 ) then : f.getScore() == 5 } } public class Frame { private int itsScore = 0 ; public int getScore() { return itsScore ; } public void add(int pins) { itsScore += pins ; } }
  4. (BNFͷ࣮૷Λগͣͭ͠ਐΊΔ class GameSpec extends Specification { when : g.add(5 )

    g.add(4 ) g.add(7 ) g.add(2 ) then : g.score() == 1 8 g.scoreForFrame(1) == 9 g.scoreForFrame(2) == 1 8 } public class Game { private int itsScore = 0 ; private int[] itsThrows = new int[21] ; private int itsCurrentThrow = 0 ; public int score() { return itsScore ; } public void add(int pins) { itsThrows[itsCurrentThrow++] = pins ; itsScore += pins ; } public int scoreForFrame(int frame) { int score = 0 ; for (int ball = 0; frame > 0 && (ball < itsCurrentThrow) ; ball += 2, frame--) { score += itsThrows[ball] + itsThrows[ball + 1] ; } return score ; } }
  5. w ୯ͳΔ഑ྻͰ༻Λ଍͢ͳΒɺͦΕͰ0, w ઃܭ্ͷ൑அΛ஗ΒͤΔ γϯϓϧͳઃܭΛબ୒͠Α͏ public class Game { private

    int itsScore = 0 ; private int[] itsThrows = new int[21] ; private int itsCurrentThrow = 0 ;
  6. w ςετ͕͋Δ͔Β҆৺ϦϑΝΫλϦϯά ৗʹϦϑΝΫλϦϯά͠Α͏ public int scoreForFrame(int frame) { int score

    = 0 ; for (int ball = 0; frame > 0 && (ball < itsCurrentThrow) ; ball += 2, frame--) { score += itsThrows[ball] + itsThrows[ball + 1] ; } return score ; } public int scoreForFrame(int theFrame) { int ball = 0 ; int score = 0 ; for (int currentFrame = 0; currentFrame < theFrame ; currentFrame++) { int firstThrow = itsThrows[ball++] ; int secondThrow = itsThrows[ball++] ; score += firstThrow + secondThrow ; } return score ; }
  7. if (strike()) { score += 10 + nextTwoBallsForStrike() ; ball++

    ; } else if (spare()) { score += 10 + nextBallForSpare() ; ball+=2 ; } else { score += handleSecondThrow() ; } l͜ͷTDPSF'PS'SBNFؔ਺Λݟͯ͝ΒΜΑʂ ͜Εͦ͜Ϙ΢ϦϯάͷϧʔϧΛ͖ΘΊͯ؆ܿ ʹهड़ͨ࢟ͩ͠Αʂz
  8. ͲͪΒͷઃܭ͕͙͢Ε͍ͯΔ͔ʁ if (strike()) { score += 10 + nextTwoBallsForStrike() ;

    ball++ ; } else if (spare()) { score += 10 + nextBallForSpare() ; ball+=2 ; } else { score += handleSecondThrow() ; } ࣄલͷઃܭ 5%%ʹΑΔઃܭ
  9. ϞοΫΛ࢖ͬͨςετ private Game g private Scorer s def setup() {

    s = Mock() // ScorerͷϞοΫ g = new Game(s ) } def "2ճ౤ٿͨ͠ͱ͖ͷείΞ͕ਖ਼͍͠"() { when : g.add(5 ) g.add(4 ) def score = g.score( ) then : score == 9 1 * s.addThrow(5 ) 1 * s.addThrow(4 ) 1 * s.scoreForFrame(2) >> 9 } ؒ઀ೖྗ ؒ઀ग़ྗ ʨ ૬ޓ࡞༻ͷݕূ