TDDã®ãµã¤ã¯ã«ã¯Red-Green-Refactoringã§ãããã®ãªãã¡ã¯ã¿ãªã³ã°ã¯ããããã¯ãã³ã¼ãã¯ãã¡ããã§ããããã¹ãã«ãé©ç¨ããã¾ããããããã¹ãã®ãªãã¡ã¯ã¿ãªã³ã°ã«ã¯ããããã¯ãã³ã¼ãã®ãªãã¡ã¯ã¿ãªã³ã°ã¨éãã¨ãããããã¾ãã
ãããã¯ãã³ã¼ãã®ãªãã¡ã¯ã¿ãªã³ã°ã¯ãä¸è¨ã§è¨ãã°å¾ã§å¤æ´ããã¨ãã®ããã«ãããã®ã§ãããããã¯ãã³ã¼ããå¤æ´ããã®ã¯ãæ©è½è¿½å ãå¤æ´ãåé¡ã®ä¿®æ£ã®ããã§ããã¤ã¾ããå°æ¥ãããã¯ãã³ã¼ãã®ä¾¡å¤ãé«ããããã«ãªãã¡ã¯ã¿ãªã³ã°ããã¾ãã
ãã¹ãã®ãªãã¡ã¯ã¿ãªã³ã°ãããã¡ããå¤æ´ã®ããã§ããããã¹ãã³ã¼ãã®å¤æ´ã¯ãããã¯ãã³ã¼ãã®å¤æ´ã«è¿½éãã¦çºçãã¾ãããã¹ãå 容ãå¤æ´ããããã«ãã¹ãã³ã¼ããç´ããã¨ã¯ç¨ã§ãããããã£ã¦ããããã¯ãã³ã¼ããå¤æ´ããããããããã«ããã¹ãããªãã¡ã¯ã¿ãªã³ã°ããã
é常ã®(ã¤ã¾ããããã¯ãã³ã¼ãã®)ãªãã¡ã¯ã¿ãªã³ã°ã¯ãã¾ãã«ãªãã¡ã¯ã¿ãªã³ã°ãã¦ãããã®ã³ã¼ããããããããããã«ãããªãã¾ãããã¹ãã®ãªãã¡ã¯ã¿ãªã³ã°ãåãæè¦ã§ãã£ã¦ãã¾ãã¨ããã¹ããã®ãã®ã¯å¤æ´ãããããªã£ã¦ãããããã¯ãã³ã¼ããå¤æ´ããã¨ãã«å¯¾å¿ããå½¹ã«ã¯ç«ããªããä¸æãããã¨ããã£ã¦å¤§å¤ã«ãªã£ã¦ãã¾ãããããããã¨ãèµ·ãã¾ãã
ãã¹ãã®ãªãã¡ã¯ã¿ãªã³ã°ãããã¨ããç§ãæ°ãã¤ãã¦ãããã¨ãç´¹ä»ãã¾ãã
ãã¹ãã¯æ¸ããã®ãä¸çª
ãããã¯ãã³ã¼ããå¤æ´ããããã«ããã¹ãã³ã¼ãã®å¤æ´ãçºçãããã¨ãããã¨ã¯ããã¹ãã³ã¼ãã¯å°ãªãæ¹ãããããã¯ãã³ã¼ãå¤æ´ã®ã³ã¹ããæ¸ãã¾ããã¨ãããã¨ã¯ãåç´ã«ãã¹ããæ¸ããã°å¤æ´ã«å¼·ããªãã¾ããä¸è¦ãªãã¹ãã¯æ¶ãã¦ãããããããã¹ãã®ãªãã¡ã¯ã¿ãªã³ã°ã§ã¯ä¸çªç°¡åã§ãå¹æãé«ããã®ã§ãã
TDDãã¡ããã¨ãã£ã¦ãããä¸éãå®æããã¨ãä¸é£ã®ãã¹ããæ®ãã¾ãããã®ä¸ã«ã¯ãããã¯ãã³ã¼ãã®æ©è½ãæ ä¿ãããã¹ãã¨ãã¦è¦ãã¨ãã価å¤ã®ä½ããã®ãããã¾ãã
- ç°å¸¸ç³»ãã«ãã¼ãã¦ããããä»ã¯æ±ºãã¦çºçããªãã±ã¼ã¹
- å¤ãã調éããã¢ã¸ã¥ã¼ã«ã®æ©è½ã調ã¹ãã¨ãã«æ¸ãããã¹ã
- æå³çã«åããã¹ã(åãåå¤ã¯ã©ã¹ããããã¸ã§ã¯ãã³ã¼ãã®ãªãã¡ã¯ã¿ãªã³ã°ã«ããåãå 容ã«ãªã£ã¦ãã¾ã£ããã®)
- ã«ãã¬ãã¸ãªã©ã®ã¡ããªã¯ã¹ãæºããããã ãã«æ¸ãããã¹ã(çºçããä¾å¤ããã¹ã¦ã«ãã¼ããããªã©)
- TDDã®ç´°ããã¹ããããå»ãããã«æ¸ãããã¹ãããå®è³ªçã«è¨èªæ©è½ã確èªãã¦ãããããªãã¹ã(newããçµæãnullã§ãªããã¨ã確èªãããã³ã¬ã¯ã·ã§ã³ãã¤ãã¬ã¼ã¿ã®æåã確èªãããåç´ãªSQLæããã¹ãããããªã©)
ãããããã¹ãã¯ãåé¡ãèµ·ããªãç¯å²ã§æ¶ãã¦ãã¾ãã¾ãããããã¡ããé£ããã®ã¯ãåé¡ãèµ·ããªãç¯å²ãã®è¦æ¥µãã§ãããã®ãã¹ãæ¶ãã¦ã大ä¸å¤«ã ããã? ãªãã§ãããªãã¹ãããããã ã£ã? ãã£ã¨å¾ã§ãã°ä½ãè¾¼ã¿ã®åå ã«ãªã£ããããªãã ããã? ããä¼¼ããã¹ãããããã©ãã¡ãã£ã¨ã ãéããã ãããã©ã£ã¡ãæ®ããã? ãªã©ãªã©ãããæå³ã§ã¯ããã¹ããæ¸ãããæ¶ãæ¹ããã£ã½ã©æéã¨ç¥çµã使ãã¾ãã
æ¶ãããããã©èªä¿¡ããªããªãå®å ¨å¯ãã«åãã¦ãæ¶ããªãã§ãããããããããã¾ããæ¶ãã¦ã大ä¸å¤«ãªãã®ã¯ãã ãããããã«è¦ã¤ãããã¾ããèªä¿¡ãæã¦ãªãã¨ãã¯ãå¾ã ã«ãã¦ãªã«ãé ããçç±ããããã®ã§ããæ¶ããã¨ã¯å¾ã§ãã§ããããæ¶ããã¨ãä¸æçã«éå®è¡ç¶æ ã§æ®ãã¦ãããã¨ãã§ãã¾ããèªåã§ã¯å¤æãã§ããªãã¦ããä»ã«ç¥ã£ã¦ã人ãããããããã¾ããã
å®å¿ãã¦æ¶ãããã®ã ãæ¶ãã¦ãããã¹ãã¯çµæ§ãã£ãããã¾ããæ´çããä¸ã§ãã¹ãè¨è¨ãç«ã¡ç¾ãããã¨ãããã¾ããã¾ãã¯ãæ¶ãããã¹ããæ¢ããã¨ããè¦ç¹ã§ãã¹ããè¦ç´ãã¨ããã§ãããã
è¦ãã¦ããã®ã¯ä¸æ®µéã¾ã§
ãã¹ãã¯èªã¿ãããã大äºã§ãããããã¯ãã³ã¼ããè¤éãªãããã®ä¸ã«ã¯è¦æ±ã®è¤éããåæ ããã¦ãã¦ããããã¯ããææ¡ããå©ãã«ãªãã¾ãããã¹ãã³ã¼ããè¤éãªã®ã¯æéãç¡é§ã«ããã ãã§ãããã¹ãã³ã¼ããèªãã¨ãã¯ãå¿ è¦ãªæ å ±ã«ãããã£ã¦ã以ä¸ã®é ã§èªãã§ããã¾ãã
- ãã¹ãã®ååãããã§åããã°ãããä¸çª
- ã¢ãµã¼ãé¨åãååã§ã³ã³ããã¹ãã確èªå 容ããããã°ãã¢ãµã¼ãé¨åãèªãã°æ£ç¢ºãªå 容ãã¤ããã¾ã
- ãã£ã¯ã¹ãã£(ã³ã³ããã¹ã)é¨åãããã¾ã§èªãã®ã¯ç¶æ³ãããã¨ããããããã¨ãã§ã(ãããã°ä¸ã¨ã)
ããã ãã®ãã¨ãææ¡ããã®ã«ãããã¦ãã¹ãã¡ã½ããã®ä¸ã ãèªãã°ãããããã«ãªã£ã¦ãã¦ã»ããã§ããä½ããã®å¦çããã¹ãéã§å ±éåãã¦ããã¨ãããå ±éåããé¢æ°ã»ã¡ã½ãããååããããããååã«ãªã£ã¦ããã°ãä¸ã¾ã§èªã¾ãã«æ¸ã¿ã¾ããå¿ è¦ã«ãªã£ããã¡ã½ããã®ä¸èº«ã¾ã§è¦ã¾ãããå ±éåã§é å¼µãããã¦ããã¨ã1ãã¹ãã¡ã½ãããææ¡ããããã«ã©ãã¾ã§ãæ·±å ¥ããã¦ãã¾ãã¾ã(å³1)ã
å³1 å
±éåã§é å¼µãã¨ãèªãã®ã大å¤
ãããªãã¨ããã¹ããèªãã®ã«ã©ãã ãæéãããããåããã¾ãããåãã£ã¦äºæ¸¬ãããã¨ãã§ããªãããèªãã§ããæä¸ããã¨ã©ãã ãæ®ã£ã¦ãã®ãè¦å½ãã¤ãã¾ãããæ£ç´ãããªã«é å¼µããããªãã¨ããæ°æã¡ã«ããªãã¾ãããã ãã ãä½ã®ããã«ã©ããèªããã¨ãã¦ããã®ãããããªããªã£ããããã¾ãã
å ±éåãããå ´åã¯ãä¸æ®µéã¾ã§ããã以ä¸è³å ã¹ã¿ãã¯ã使ããªãã§æ¸ãããã«ãã¾ããã(å³2)ããããªãã°ãå¤å°ã®èª¤å·®ã¯ãããèªãã¹ãåéã®äºæ¸¬ãã§ãã¾ãã
å³2 å
±éåãä¸æ®µéã ã¨ãå
ãè¦éãããã
ãã¹ã対象ã®ãªãã¸ã§ã¯ããè¤éã ã¨ããã£ã¯ã¹ãã£ãæºåããããã«å¤§éã®å®è£ ãå¿ è¦ã«ãªã£ã¦ããã®ããã®å ±éåããããã¦ãã¾ããã¨ãããã¾ãããã¹ãã±ã¼ã¹ã®æ°ãå¤ãã¨ããããããããªããªã¨ã¼ã·ã§ã³ã«å¯¾å¿ããããã«è¤éãªå ±éåããããã¡ã§ãããã©ã¡ã¿åãã¹ãã§ãããã©ã¡ã¿ãæºåããå¦çãå¤ããªããã¨ãããã¾ãããã¹ããæ¸ãã¨ãã«ã¯ä¾¿å©ãªçååã®å·¥å¤«ããèªãã¨ãã«ã¯éªéã«ãªã£ã¦ãã¾ãã®ã§ãã
ã¢ãµã¼ãé¨åã¯ã¾ã¨ããªã
å ±éåãã話ã®ç¶ãã§ãããã¢ãµã¼ãé¨åããã¹ãã¡ã½ããå ã§é·ããªã£ãããè¤æ°ã®ãã¹ãã±ã¼ã¹ã§åããããªã¢ãµã¼ããæ¸ããã¨ãããã¾ããããã¾ãå ±éåããããªãã¾ãããã¢ãµã¼ãé¨åãã¾ã¨ãã¦å ±éåããã¨ãã¯ç´°å¿ã®æ³¨æãæãã¾ãããã
ã¢ãµã¼ãã«å¤±æããã¨ãã«ã¯ããã¹ãã£ã³ã°ãã¬ã¼ã ã¯ã¼ã¯ã«ããã¾ãããã©ãã§ä½ãèµ·ããã®ã説æã表示ãã¦ããã¾ãããããå ±éåããã¡ã½ããã®ä¸ã ã¨ãã©ã®ãã¹ããã©ãããçç±ã§å¤±æãããããããªããªã£ã¦ãã¾ãã¾ãããã¹ãã失æããçç±ããããã¬ã§èª¿ã¹ããªã©ã¨ãããã«ãã«ããä½æ¥ããã¦ã¯ããã¾ãããã¢ãµã¼ãã§å¤±æããã¨ãã®è¡¨ç¤ºãå ·ä½çã§æç¨ã«ãªãããã«ãã¦ããã¾ãããã
è¤æ°ã®ã¢ãµã¼ããã¾ã¨ãã¦å ±éåããã¨ãçµæçã«ãªã«ãã¢ãµã¼ããããã®ãç¦ç¹ãã¼ãããããªãã¾ããå ±éåããã¨ãããããã®ãã¹ãã±ã¼ã¹ã§ç¢ºèªãããå 容ããã¹ã¦ã¾ã¨ãã¦ã¢ãµã¼ããããã¨ã«ãªãã¾ãããã®çµæãæ¬æ¥ãã¹ãã±ã¼ã¹å ã§ã¢ãµã¼ãããå¿ è¦ããªãç®æã¾ã§ã¢ãµã¼ããããã¨ã«ãªã£ã¦ãã¾ãã¾ããããã§ã¯ãã¹ããã©ãã«æ³¨ç®ãã¦ããã®ãããããªããªããå¯èªæ§ãä¸ãã¾ãããããã¯ãã³ã¼ãã®å¤æ´ã«ãå¼±ããªãã¾ãã
ã¢ãµã¼ãã®é¨åãå ±éåããã¨ãã¯ããã¬ã¼ã ã¯ã¼ã¯ãæä¾ããã¢ãµã¼ãã¨åã¬ãã«ã®ã¢ãµã¼ãã¡ã½ãããç¬èªã«å®è£ ãã¦ãä¸éå端ãªå ±éåã«çµãããªãããã«ãã¾ããHamcrestãªã©ã®Matcherã使ããã¹ãã§ããã°ãã«ã¹ã¿ã Matcherãå®è£ ããã°åé¡ã¯èµ·ãã«ãããªãã¾ãã
ãããã¯ãã³ã¼ãã«ä¾åããã¨æ»ã¬
- å ¥å(ãã¨ãã°æåå)ãåãåã£ã¦å é¨æ§é ã«ãã¼ã¹ãã
- ä¸ã§å é¨æ§é ã®å 容ãæ´æ°ãã
- å é¨æ§é ãåºåå½¢å¼(ãã¨ãã°æåå)ã«ãã©ã¼ããããã¦åºåãã
ãããããã¿ã¼ã³ã®æ©è½ãããã¨ãã¾ã(ããããã¾ãã)ãåç´ã«èãã¦ã以ä¸ã®ãããªãã¹ããæ¸ããã¨ã«ãªãã¾ãã
i. å
¥åã渡ãã¦ãå
é¨æ§é ãåãåºããã確èªãã
ii. æ´æ°å
容ã確èªãã
iii. å
é¨æ§é ã渡ãã¦ãåºåãã©ã¼ããããæ£ããã確èªãã
ãã®ã¨ããi.ã§ã¯å é¨æ§é ã®å 容ãã¢ãµã¼ããããã®ã§ããããã®ã¾ã¾å é¨æ§é ãã¢ã¯ã»ã¹ããã®ã¯é¢åã ã£ãããã¾ããæååã«ãªã£ã¦ãã°ããã¢ãµã¼ãã§ããã®ã«ãªã...ãã3.ã®å¦çã使ãã°å é¨æ§é ãæåååã§ãããããªãã!
åããã¨ã¯ãiii.ã®ãã¹ãã§ãè¨ãã¾ãããã¹ãã®ããã«å é¨æ§é ãæºåããã®ã«ã1.ã®æ©è½ã使ãã°æååããç°¡åã«å é¨æ§é ãæºåã§ãã¾ããã
ãããã¦ã次ã®ãããªãã¹ãã³ã¼ããæ¸ãã¦ãã¾ãã¾ãã
1ã®æ©è½ã®ãã¹ã() { // i.å ¥åã渡ãã¦ãå é¨æ§é ãåãåºããã確èªãã å é¨æ§é = 1.ã®å¦ç(ãã¹ãç¨ãã¼ã¿ã®æåå表ç¾) çµæ = 3.ã®å¦ç(å é¨æ§é ) assertEquals(çµæ, ãã¹ãç¨ãã¼ã¿ã®æåå表ç¾) } 3ã®æ©è½ã®ãã¹ã() { // iii. å é¨æ§é ã渡ãã¦ãåºåãã©ã¼ããããæ£ããã確èªãã å é¨æ§é = 1.ã®å¦ç(ãã¹ãç¨ãã¼ã¿ã®æåå表ç¾) åºå = 3.ã®å¦ç(å é¨æ§é ) assertEquals(åºå , ãã¹ãç¨ãã¼ã¿ã®æåå表ç¾) }
ãªãã¨ãã¹ãã¡ã½ãããåãã«ãªã£ã¦ãã¾ã£ã! ããã¹ãã¯æ¸ããã®ãä¸çªãã®ç²¾ç¥ã«ã®ã£ã¨ã£ã¦ãã²ã¨ã¤ã«ã¾ã¨ãã¦ãã¾ãã!
...ãªãã ãå¤ã§ãããããã¨ãã¨ã¯å ¥åã¨åºåããããã®ãã¹ãã ã£ãã®ã«ããå ¥åãããã®ãåºåããã¨åãã«ãªããã¨ãããã¹ãã«ããæ¿ãã£ã¦ãã¾ãã1.ã¨3.ã®å¦çã«åããã°ããã£ããæ¤åºã§ãã¾ããã(両è ãåã人ãæ¸ãã¦ãããããã£ã¨åãããã«ééããã§ããã)ãä»æ§ãå¤ãã£ã¦å ¥åºåãé対称ã«ãªã£ããä¸æ°ã«ç ´ç¶»ãã¦ãã¾ãã¾ãã
ãã¹ãã®ä¸ã§ããã¹ãã®ããã«ãããã¯ãã³ã¼ãã使ãããã欲æ±ã¯ããããã¾ãããã¹ãã®ä¸ã§ããããã¯ãã³ã¼ãã¨ãã£ããåãã³ã¼ããæ¸ããã¡ã«ãªããã¨ããã¾ã¾ããã¾ãã確ãã«ãã¹ããæ¸ãæéã¯çãããããããã¯ãã³ã¼ãã®(ãã£ã¨ã¡ããã¨ãã¹ãæ¸ã®)æ©è½ãå¼ã³åºãã°ãå®å ¨ã ãèªã¿ããããããã¾ãã
ããããããã¯ãã³ã¼ãã«ãã¹ããä¾åãã¦ãã¾ãã¨ããããã¯ãã³ã¼ãã®å¤æ´ããã¹ãã®ä¸å¿ è¦ãªå¤æ´ã«ã¤ãªããã¾ããå ´åã«ãã£ã¦ã¯ããããã¯ãã³ã¼ãã®éãã«åãã¦ãããã¨ç¢ºèªããã ãã®ãã¹ãã«ãªã£ã¦ãã¾ããå質ãæ ä¿ã§ããªããªãã¾ãã
ãã¹ãã®ä¸ã§ãããã¯ãã³ã¼ããå©ç¨ããã®ã¯ã絶対ã«ãã¡ã¨ããããã§ã¯ããã¾ããããé¿ããæ¹ãããã§ããããä½æ¥éãç¯ç´ããã¤ãããããã¨ã§ææªã®ã¿ã¤ãã³ã°ã§å¸°ã£ã¦ãããã¨ã«ãªãããã¾ããã
以ä¸ããã¹ãããªãã¡ã¯ã¿ãªã³ã°ããã¨ãã«æ°ãã¤ããã¹ã4ã¤ã®ç¹ãç´¹ä»ãã¾ããã