ã¹ã¿ãã»ã¢ãã¯ã¯æ¬å½ã«æªè ãªã®ãï¼ããã¹ãé§åéçºãããã¦ããªãæ®ãã¹ãç¿æ £ã¨ã¯ (2)
TDDæ¹å¤ã®éã«çã£å ã«çå ãåããããã¹ã¿ãã»ã¢ãã¯ã¯æ¬å½ã«å½¹ã«ç«ããªã代ç©ãªã®ã§ããããï¼èªèº«ã®çµé¨ãè¸ã¾ãã¦åèãã¦ã¿ã¾ãã
ã¯ããã«
TDDãã¦ããããã¹ãã«ã¤ãã¦ã®æ¹å¤ã«ã¤ãã¦ã¯ãã¹ã¿ãã»ã¢ãã¯ããããã¤ãã®è¨äºã«ã§ãæ¹å¤å¯¾è±¡ã«ä¸ããã¾ãã
âå®éã«ã¢ã¸ã£ã¤ã«ãTDDã®å®è·µè ã®è©±ãæãè¿ãã¦ããå½¼ããDHHæ°ãè¨ãã¨ããã®å³å¯ãªæå³ã§ã®ã¦ããããã¹ãï¼ä¾åé¢ä¿ã¯å ¨ã¦Mockã«ãã¦ã対象ã¨ãªãã¦ãããã ãããã¹ã対象ã«ããï¼ã«ãã ãã£ã¦ããå°è±¡ã¯ãªããããããDHHæ°ã¨åæ§ãéå°ãªMockã®å°å ¥ã¯ç¡é§ãå¤ãããæ¬æ¥ãã¹ããã¹ãç®æããã¹ãåºæ¥ãªãã¨ããåé¡æèãæã£ã¦ãã人ãå¤ãããã«æããâ
ããããç§ãé ã®ï¼ï¼å¹´ã»ã©åã®ï¼ã¤ç®ã®éçºããã¸ã§ã¯ãã§ã¯ã¹ã¿ãã»ã¢ãã¯ã使ã£ããã¹ãã®ã¡ã³ããã³ã¹ã«çãç®ã«åãã¾ããã
ããã©ãããã®çãçµé¨ãããããã¨ãã£ã¦ãã¹ã¿ãã»ã¢ãã¯ã以éã®ããã¸ã§ã¯ãã§å ¨ã使ããªããªã£ãã¨ãããã¨ã¯ããã¾ããã
ä»æ¥ã¯ãã¹ã¿ãã»ã¢ãã¯ã軽ãããããããè¨è¨å¤æã®ãã³ãã¨ãªããããµã´ãã«ã¢ã¼ããã¯ãã£ã¨ãã®ç³»çµ±ãæãã¦ãã¹ã¿ãã»ã¢ãã¯ã®ä½¿ãæãæ¹ãã¦åæ¤è¨ãã¾ãã
ã¹ã¿ãã»ã¢ãã¯ã£ã¦ä½ï¼
ã¹ã¿ãã®ãããã
ãã¹ã対象ãªãã¸ã§ã¯ã(SUT)ãè¥å¹²ãããããé£äººãªãã¸ã§ã¯ã(DOC)ã¨ã³ã©ãã¬ã¼ããã¦ããåæã§ãé£äººãªãã¸ã§ã¯ãããã®éæ¥å ¥å(Indirect Input)ã«ãã£ã¦å®è¡çµæã決ã¾ãå ´åãã©ã®ããã«SUTãæå¾ éãåä½ããããæ¤è¨¼ããã°ããã§ããããï¼ãã®æ Test Stubã®åºçªã§ããTest Stubã§äºãéæ¥å ¥åï¼Indirect Inputï¼ããããå¤ããã¹ãã³ã¼ãã§æè¨ããDOCã¨å·®ãæ¿ããSUTãæå¾ éãåä½ãããã確èªãã¾ããDOCã§ã®çºçæ¡ä»¶ãé£ããå¤ãä¾å¤ç³»ã§ãã£ã¦ããTest Stubã§ããã°ãã¹ãã³ã¼ãããã³ã³ããã¼ã«ãããããSUTã®åä½ç¢ºèªãç°¡åã«è¡ãªãã¾ãã
ã¢ãã¯ã®ãããã
ãã¹ã対象(SUT)ããããããé£äººãªãã¸ã§ã¯ãï¼DOCï¼ã¨ã³ã©ãã¬ã¼ããã¦ããåæã§ãã©ããã£ã¦SUTããDOCã¸ã¨æå¾ éãã¡ãã»ã¼ã¸ã³ã°ãããã確èªããã°ããã§ããããï¼ãã®ãããªã¨ãã«Mock Objectã®åºçªã§ããMock Objectã«äºãéæ¥åºåï¼Indirect Outputï¼ã®æå¾ ããæ¯ãèãããã¹ãã³ã¼ãã§æè¨ããDOCã¨å·®ãæ¿ããSUTãæå¾ éããé£ããã¨ã³ã©ãã¬ã¼ãããããæ¤è¨¼(Verify)ãã¾ãã
ã¤ãããã®ã¢ã㯠ã®ã©ã¤ãã©ãªã¯ãªãã¸ã§ã¯ãæåã®éè¦æ¦å¿µã§ãããªãã¸ã§ã¯ãéã®ã¡ãã»ã¼ã¸ããã·ã³ã°ï¼âSUT-DOCéã®éæ¥åºåãéæ¥åºåï¼ã®æ¯ãèããç°¡æ½æçã«DSLã§è¨è¿°ã§ããããã«å·¥å¤«ããã¦ãã¾ãã
ã¹ã¿ãã»ã¢ãã¯ã«ã¤ãã¦ã¯æ¬¡ãåç §ãã¦ä¸ããã
- http://bliki-ja.github.io/TestDouble/
- http://goyoki.hatenablog.com/entry/20120301/1330608789
- https://martinfowler.com/articles/mocksArentStubs.html
ã§ã¯ãåä½ç¢ºèªããéã«ããã¹ã対象ããåãé¢ããããããããé£äººï¼DOCï¼ã¯ä¸ä½ãªã«ã«ç¸å½ããã®ã§ããããã次ã«ç´¹ä»ãããããµã´ãã«ãï¼ã¤å¤æææã«ãªãã¾ãã
ãããµã´ãã«ã¢ã¼ããã¯ãã£ãã¯ãªã¼ã³ã¢ã¼ããã¯ãã£
ã½ããã¦ã§ã¢ã®è¨è¨ã大æ ã§èãã¨ããä¸ä¸ã®ã¡ã¿ãã¡ã§æ´çããã¬ã¤ã¤ã¼ã¢ã¼ããã¯ãã£ã®ã»ããä¸å¤®ã¨å¨è¾ºã®ã¡ã¿ãã¡ã¼ã§æ´çããæ¹æ³ãããã¾ããä¸å¤®ã¨å¨è¾ºã§æ´çã¯ãã¢ãªã¹ã¿ã¼ã³ãã¼ã³ã®ãããµã´ãã«ã¢ã¼ããã¯ãã£ãæåã§ãããã®å¾ãå®è·µãã¹ãé§åéçºã The Clean Architectureã®ä¸ã§å¤å½¢ããã¦å¼ç¨ãããã¾ããã¹ã¿ãã»ã¢ãã¯ã®ä½¿ãæãæ¤è¨ããã®ã«ããã®ä¸å¤®ã¨å¨è¾ºã«ããæ´çã¯ä¾¿å©ã§ãããã¡ã¤ã³é§åè¨è¨ã®å ´åã¯ããã¡ã¤ã³ãä¸å¿ã«æ®ãããã¨ã«ãªãã¾ãã
æè¡è©³ç´°ã¯å¤å´ã¸å¯ãã
ãã¤ã³ãã¯ãä¸å¿ã¨ãªã対象ãã¡ã¤ã³ã¯ä½ãï¼ä¸å¿ããæé¤ãããè¦ç´ ã¯ä½ãï¼ãèæ ®ãã¦åºåããããã¨ã§ããä¸å¿ã®ãã¡ã¤ã³ããæé¤ãããé ç®ã®ä»£è¡¨ä¾ãããã¼ã¿ãã¼ã¹ã¢ã¯ã»ã¹ãå¤é¨Webã·ã¹ãã ãã¡ãã»ã¼ã¸ã³ã°ã¨ãã£ã詳細ã®æè¡è¦ç´ ã§ãããã¡ã¤ã³é§åè¨è¨ã®è¨è¨å¤æãåãå ¥ãã¦ããå ´åã¯ããªãã¸ã§ã¯ãã¸ã®ã¢ã¯ã»ã¹ããããã®Repositoryã®ã¤ã³ã¿ãã§ã¼ã¹ã®ã¿ãä¸å¿ãã¡ã¤ã³ã«å ¥ãè¾¼ã¿ãRepositoryã®å®è£ ï¼ç¹å®ã®ãã¼ã¿ãã¼ã¹ç¨®é¡ãSQLãªã©å®è£ 詳細ï¼ã¯å¤å´ã«è¿½ãããã¾ããåæ§ã«ãã¤ã³ã¿ãã§ã¼ã¹ã®ã¿ãä¸å¿ã«ããã¦ã¡ãã»ã¼ã¸ã³ã°ãä»ã®Webã·ã¹ãã ã®ã¢ã¯ã»ã¹çã®å®è£ ã®è©³ç´°ã¯å¤é¨ã«è¿½ãããã¾ãã
ãã¾ãåºåãã§ããã°ãä¸å¤®ã«æ®ã£ãç´ç²ãªãã¸ãã¹ã«ã¤ãã¦ã®ã«ã¼ã«ãç¶æ é·ç§»ã«ã¤ãã¦ãã¦ããããã¹ãããªãã¡ã¯ã¿ãªã³ã°ãç¶ç¶ãããã¨ãã§ãã¾ãããªãã¡ã¯ã¿ãªã³ã°ã«ããè¨è¨æ¹åãç¶ç¶ãããã¨ã§ã対象ãã¡ã¤ã³ã«ã¤ãã¦ç解ãæ·±ãããã¨ãã§ãã¾ãããã®åºåãã®ç®æã§ã¹ã¿ããã¢ãã¯ã®åºçªãã§ã¦ãã¾ãã
ã¬ã¬ã·ã¼ã·ã¹ãã ã®è©³ç´°ã¯å¤å´ã¸å¯ãã
éçºå¯¾è±¡ã®ä¸å¿ãã¡ã¤ã³ã®å¤å´ã«è¿½ãããããã®ã¯æè¡è¦ç´ ã«éãã¾ãããé£æºäºå®ã®å¤ãä»ã·ã¹ãã ã®ãã¡ã¤ã³ã®èªå½ä½ç³»ãå«ã¾ãã¾ãã å¤ãã·ã¹ãã ã®ãã¡ã¤ã³ã®èªå½ä½ç³»ã¨éçºå¯¾è±¡ã®ãã¡ã¤ã³ã®èªå½ä½ç³»ãæ··ããã¨æ··ä¹±ãã¾ããåé³ç°ç¾©èªã®ã¯ã©ã¹ãã¡ã½ãããããããã£ãæ··ããè¾¼ãã¨ç®ãå½ã¦ããã¾ããããã®ãããªæã¯ï¼ã¤ã®ãã¡ã¤ã³ã®å¤æã¬ã¤ã¤ã¼ãè¨ããéçºå¯¾è±¡ã®ä¸å¿ãã¡ã¤ã³ããã¬ã¬ã·ã¼ã·ã¹ãã ã®èªå½ä½ç³»ã§æ±æãããªãããå¤å´ã«è¿½ãããã¾ãããã®åºåãã®ç®æãã¹ã¿ããã¢ãã¯ã®åºçªãèãããã¾ããè æé²æ¢å±¤ã«ã¤ãã¦ã¯æ¸ç±ãã¡ã¤ã³é§åè¨è¨ãä¸è¨ãåç §ã«ãã¦ä¸ããã
https://www.ogis-ri.co.jp/otc/hiroba/technical/DDDEssence/chap3.html#AnticorruptionLayer
ãã®ã»ãããã¸ãã¹ãã¡ã¤ã³ã®ä¸ã§ãããã¸ãã¹ã«ã¼ã«ã®ä»æ§ã¨è¤éãªã¢ã«ã´ãªãºã ã®å®ç¾æ段ãåé¢ããããªãããããã¾ããã
ãã¡ã¤ã³/å¨è¾ºç°å¢ã®è¤éãã«ãã£ã¦ã¢ãã¯ã¹ã¿ãã®ä½¿ãæãã¤ã³ãã¯å¤ããï¼
ã¹ã¿ãã»ã¢ãã¯ã®ä½¿ãæã®å¤æãã¤ã³ããå¹¾ã¤ãããã¾ãããã¨ãã°ããä¸å¤®ã®ãã¡ã¤ã³ã¯è¤éãï¼ããå¨å²ã®ç°å¢ã¯å¤æ§ããæºåã³ã¹ãã¯é«ä¾¡ãï¼ããç¹°ãè¿ããã¹ãã¯å¯è½ããããã¹ããå©ç¨ãã¦ç解ãæ·±ããããã¨ã¯ãªã«ãï¼ããªã©ã§ãã
Aæ¡ï¼ä¸å¤®ã¨å¨è¾ºã®åé¢ãæ¶æ¥µçã«è¡ãä½æ¦
ãããéçºå¯¾è±¡ããä¸å¤®ã®ãã¡ã¤ã³ã¯è¤éã§ã¯ãªãã大ãããã¸ãã¹ã«ã¼ã«ããç¶æ é·ç§»ããªããæ³å®ã¢ã¯ã¿ã¼ãï¼ã¤ãããå¨è¾ºã®æè¡è¦ç´ ã®ç»å ´è¦ç´ ã¯å°ãªããå¤ããäºå®ããªããããå¨è¾ºç°å¢ã®æºåã¯ç°¡åã»å®ä¾¡ã§ããã¼ã«ãæã£ã¦ããAll Test Greenãä¿ã¤è¦è¾¼ã¿ããããããçµåãã¹ãã®ç·ä»¶æ°ã¯å°ãããç¹°ãè¿ãå®è¡ã³ã¹ããå°ããï¼ä»¶æ°ããããã¹ãã®ä¸¦ååãå¯è½ï¼ãããªã®ã§ããã°ããããµã´ãã«ã¢ã¼ããã¯ãã£ç³»çµ±ã®è¨è¨å¤æã®å¿ è¦æ§ã¯ä½ããªã£ã¦ããã¾ãã
ä¸å¿ãã¡ã¤ã³ã¯è¤éã§ã¯ãªãå¨è¾ºç°å¢ãè¤éã§ã¯ãªããçµåãã¹ã件æ°ãéãããã®ã§ããã°ãã¢ãã¯ã¹ã¿ãã®åºçªãæ¸ãããã©ã³ã¶ã¯ã·ã§ã³ã¹ã¯ãªããã§ãµã©ãã¨ã¤ãã£ã¦ãã¾ããUI or RESTã®å ¥ãå£ã®å¤å´ãããã¼ã¿ã¼ãã¼ã¹ã¾ã§çµåãã¦æ©è½æ§ãç¶ç¶ãã¦æ ä¿ã§ãã¦ãããã«æ³¨åããåæ©ã®ã»ããå¼·ããªãã§ãããããããããããã¹ãé§åã§ããããSTEP BY STEPã§è©¦è¡é¯èª¤ãã¦ãç解ããããï¼ä¿®æ£ãããã&ãã¹ãããããæ§é ã¨æ¯ãèããçºè¦ããªããã¤ããä½æ¦ããããLoopBackã®ãããªãã¬ã¼ã ã¯ã¼ã¯ã®æµåã«åã£ã¦ãæ®ã©ããã°ã©ãã³ã°ããªãã§RESTåºå ¥ãå£ãããã¼ã¿ãã¼ã¹ã¾ã§åä½ãããã®ããããã¨ã¤ãã£ã¦ãã¾ãä½æ¦ã®æ¹ããã¿ã¼ããããã¾ããã
çã«ãããå¨è¾ºè¦ç´ Aã®ç»å ´ã«æ³¨æï¼
ãã ãããã©ã³ã¶ã¯ã·ã§ã³ã¹ã¯ãªããã®ä½æ¦ã§ãã¼ã¿ãã¼ã¹ã¨å¯ãªé¢ä¿ã®ã¤ããã¯è¨±å®¹ãã¦ããå¤é¨ã·ã¹ãã ã«ä¾åããå ´åã¯çãªé¢ä¿ã§ä½ããããªãåæ©ãçã¾ãã¾ãããã®é¨åã«é¢ãã¦ã¯ãã¹ã¿ãã»ã¢ãã¯ã®åºçªãã§ã¦ãã¾ãã
è¦ç¹ãä¸å¤®ããå¨è¾ºã«ç§»ã
ä¸å¤®ãã¡ã¤ã³ã®å¤å´ã«é ç½®ãããä»ã·ã¹ãã ãAndroidãPolymerãReactã¨ãã£ãããã³ãé¨åã«è¦ç¹ã移ãã¨ã©ãã§ãããããããã¯ã¨ã³ãé¨åã¨ç¬ç«ãã¦ä½æ¥ããããããã®åæ©ãçã¾ãã¾ãããã®é¨åããã¹ã¿ãã»ã¢ãã¯ã®åºçªã®ä½å°ãããã¾ãã
å¢çã®åãæ¹ã¨ãã¦ããã¡ã¤ã³é§åè¨è¨ã®Repositoryã®ãããªãã¡ã¤ã³å¯ãã®ã¤ã³ã¿ãã§ã¼ã¹ãç¨æããªãå ´åããRESTã®ã¤ã³ã¿ãã§ã¼ã¹ãããã³ãã¨ããã¯ã®å¢çç·ã¨ã¿ãªããã©ã¤ãã©ãªã¼ããã¬ã¼ã ã¯ã¼ã¯ãæä¾ãããã¹ãæ¯æ´ãã¼ã«ã®æµåã«åã£ã¦ï¼ä¾ãã°ãTestRestTemplate, webmockï¼å·®ãæ¿ããä½æ¦ãã¨ããã¨ã«ãªãã§ãããã
Bæ¡ï¼ä¸å¤®ã¨å¨è¾ºã®åé¢ãç©æ¥µçã«è¡ãä½æ¦
ä¸æ¹ã対象ã®ãã¸ãã¹ãã¡ã¤ã³ãè¤éãã¤å¨è¾ºç°å¢ãå¤æ§ã§è¤éãªå ´åã¯ã©ãã§ããããã
ä¸å¤®ãã¡ã¤ã³ãè¤éã§å¨è¾ºãè¤éãªã®ã§ããã°ãè¤éãªå¨è¾ºç°å¢ã¨ã¯åãã¦è¤éãªä¸å¤®ãã¡ã¤ã³ãã¿ã¼ã²ããã«ããã¡ã¤ã³ã«å¯¾ããæ´å¯ãæ·±ããããã®ã¢ããªã³ã°ã»ãã¹ãã»ãªãã¡ã¯ã¿ãªã³ã°ãè¡ãããåæ©ãçã¾ãã¾ããã¦ããããã¹ããã¹ã¿ãï¼ã¢ãã¯ã®åºçªãå¢ãã¦ãã¾ãã
ä¾ãã°ããªãã£ã¹è¤åæ©ã§åãã¢ããªã±ã¼ã·ã§ã³éçºã®å ´åãèãã¦ã¿ã¾ããããEnd2Endã§æ©è½æ§ã確èªããã«ã¯ãè¤åæ©å®æ©ã®ã»ãã«ãããã¯ã¼ã¯éä¿¡ããã¡ã¤ã«ã·ã¹ãã çãå¿ è¦ã«ãªã£ã¦ãã¾ããè¤åæ©å®æ©èªä½ã®è£½åããªã¨ã¼ã·ã§ã³ãå¤æ°ããã¾ãããã å ¨ãã¹ããã¿ã¼ã³ããã¹ã¦ã®ç°å¢è¾¼ã¿ã§åä½ç¢ºèªããã¨ãªãã¨ãæºåããæéããããããã¾ããè¤åæ©ãå¥ãã¼ã ãä½æä¸ã®å¤é¨ãµã¼ãã¹ã¨é£æºãå¿ è¦ãªã®ã§ããã°ã調æ´ã³ã¹ãããããããããã¾ãããè¤åæ©ã¢ããªã®ä¸å¿ãã¡ã¤ã³ã«æ³¨åãã¦ãã¹ãã¨ãªãã¡ã¯ã¿ãªã³ã°ã®è©¦è¡é¯èª¤ã§ããç¶æ³ãä½ããããªãã§ãããã
ã¹ã¿ãã»ã¢ãã¯ããããªãèªèº«ãé¨ãã
ãã¹ã¦ã®ææ³ã«ã¯ãã¡ãªãããã¡ãªãããããã¾ããã¦ãããã¬ãã«ã®ãã¹ãã®ã¿ã«ç±ä¸ããã¨ç²ç¹ãçã¾ããã¡ã§ããéçºè ãªãã¦ããããã¹ã対象ã®ãªãã¸ã§ã¯ãï¼SUTï¼ã®å¨è¾ºã®åæãééã£ãã¹ã¿ãã»ã¢ãã¯ãä¸åº¦ã¯ä½ã£ããã¨ããã®ã§ã¯ãªãã§ããããï¼
ã¦ããããã¹ãã§ã¯ç°å¢è¾¼ã¿ã§çµåãã¦åä½ãæ ä¿ãç¶ãããã¨ã¯ä¿è¨¼ãã¦ããã¾ãããã¦ããããã¹ãã¯ãè² è·ãã¹ããæ¢ç´¢çãã¹ããã¦ã¼ã¶ããªãã£ãã¹ããªã©ãªã©ã®ä»£ããã«ã¯ãªãã¾ãããã¹ã¿ã¼ãã¢ããã®åæã«ç¥ãããããããã¯ããå¸å ´ã«ãã£ãããããã®å¤æã®æ å ±ã¯ã¦ããããã¹ãã§ã¯æä¾ãã¦ããã¾ããããããã¯ãã³ã¼ãã«ã¤ãã¦ç§éãç¥ããªããã¨ããã£ããä½ã§ãä½ãããã°ç¥èãç²å¾ã§ããã®ããèªåãã¡ã§èãã¦ãå®é¨ãç¹°ãè¿ãã¦è¡ãå¿ è¦ãããã¾ãã
ã¾ã¨ã
ã¹ã¿ãã¨ã¢ãã¯ã®ããããããã¦ããããµã´ãã«ã¢ã¼ããã¯ãã£ãç´¹ä»ããã¹ã¿ãã»ã¢ãã¯ã®ä½¿ãæãåæ¤è¨ãã¾ããã
ä¸å¿ãã¡ã¤ã³ãè¤éã§å¨è¾ºç°å¢ãè¤éã§ããã°ãä¸å¤®ã¨å¨è¾ºã®ç°å¢ãåã£ã¦ä¸å¤®ã®æ´å¯ãæ·±ããããããã«ãã¾ããä¸å¤®ãå¨è¾ºãè¤éã§ãªãå¿ è¦ãªãã¨å¤æãããªããã©ã³ã¶ã¯ã·ã§ã³ã¹ã¯ãªããã§ãã¸ãã¹ãã¸ãã¯ã¨å¨è¾ºç°å¢ï¼ä¾ãã°ãã¼ã¿ãã¼ã¹ï¼ãä¸ä½ã«æ¸ãã¦çºãã¦ãã¹ãããä½æ¦ã®ã»ããæã£åãæ©ãã§ããããå¾è ã®å ´åã¯ã¢ãã¯ãã¹ã¿ãã®åºçªã¯å°ãªããªãã¾ãã
ãã ããã¹ã¿ãã»ã¢ãã¯ã使ã£ãã¦ããããã¹ãã«ç±ä¸ãããã¾ãå¤å´ã®åº§è¦ãçãã«ãªãã¨ãå¤å´ã®ç°å¢ã¨ã®å ¥åºåãã¿ã¼ã³ãæãè½ã¡ãããæ©è½æ§ã§éæããããã¨ãä½ãã誤解ãã¦ä½ã£ã¦ãã¾ã£ãããªããã¡ã§ãã並è¡ãã¦å¥ã®ãã¹ããåããã¦è¡ãªãããã¨ããã§ãã
次åã¯ãè½ã¨ãç©´ã«é¥ããªãããã«ãå®è·µãã¹ãé§åéçºã¨å®è·µã¢ã¸ã£ã¤ã«ãã¹ããåèã«å¤éã®å¦ç¿ãã£ã¼ãããã¯ã«ã¼ãã«ããç¥èç²å¾ã®ä»çµã¿ã«ã¤ãã¦è§£èª¬ãã¾ãã