Swiftã§ã¯ãæä½æ¥ã§ã¢ãã¯ãªãã¸ã§ã¯ããä½æãã¾ãããã®è¨è¨ãã©ã®ããã«ã¦ããããã¹ããæ¸ãã°è¯ããã¨ããæ¹éã決ãã¦ãã¾ãããã¹ãããã表ç¾åè±ãã«ããããã¢ãã¯ãªãã¸ã§ã¯ããããå¼·åã«ãããã¨ã¯ã§ããã§ããããï¼ã¢ãã¯ã©ã¤ãã©ãªããä½ãå¦ã¶ãã¨ãã§ããã§ããããï¼OCMockitoã¨ããObjective-C製ã®ã¢ãã¯ã©ã¤ãã©ãªãæ¸ãã¦ããç§ã®çµé¨ãSwiftã®æä½ãã¢ãã¯ã«å¿ç¨ãã¦ã¿ã¾ãã
ã¢ãã¯ãªãã¸ã§ã¯ãããã便å©ã«ãã
ã¢ã¡ãªã«ã³ã»ã¨ãã¹ãã¬ã¹ã®iOSã¨ã³ã¸ãã¢ã§ããSwiftç°å¢ã§ã¢ãã¯ãªãã¸ã§ã¯ããã©ãä½ãã®ã
ãªãå®ãªãã¸ã§ã¯ãã®ä»£ããã«ç¨æããã®ãï¼
ã³ãã¯ã¯æéãæãã¾ãã常ã«ããã¨ã¯éãã¾ããã
UnitTestã§ã¯å½ã®ã³ãã¯ã使ãããããã§ãã
æçããã¹ãããã®ã§ã¯ãªããæçé·ã«Waiterãã©ã®ããã«æ¥ããããå¥ç´é¢ä¿ãã¤ã³ã¿ã©ã¯ã·ã§ã³ããã¹ããã¾ãã
ãããã³ã«ãå®ç¾©ãããã¨ã§å¯è½ã«ãªãã¾ãã
ãªã¢ã«ãªãã¸ã§ã¯ãã§ãFAKEãªãã¸ã§ã¯ãã§ããããã³ã«ã«é©åãã¦ããã°ã©ã¡ãã使ããããã«ãªãã¾ãã
ã©ã¼ã¡ã³ãä½æãããããã³ã«ãããã¨ãã¾ãã
æçã¯æéãæããã®ã§é常ã³ã³ããªã¼ã·ã§ã³ãã³ãã©ã¼ãå¿ è¦ã§ãããã¨ããããå¿ãã¦ä¸ããã
ãããWaiterã®å®è£ ã§ããcookã¯ã¤ãã·ã£ã©ã¤ãºæã«æä¾ããã¾ãã
ããã§ã¯ã¨ããããã注æã¡ã½ããããã¼ãã³ã¼ãã£ã³ã°ãã¾ãã
å³åã©ã¼ã¡ã³2æ¯ãããã¨åµããããã³ã°ã
ã©ããã¹ãããã®ã
ã¢ãã¯ã¯ãã¯ãä½ãã¾ããã¯ãã¯ã©ã¼ã¡ã³ã¡ã½ããã®å®è£ ã§ããå®éã«æçã¯ããã¾ããã
æ å ±ãåå¾ãã¾ããã©ããã£ã¦å¼ã³åºããããã確ããã¾ããããã¯ã¢ãã¯ãªãã¸ã§ã¯ãã®éè¦ãªãã¨ã§ãã
ãã¹ããããã¡ã½ãããå¼ã³åºãã¾ããorderã¡ã½ããã§ãã
æçµçã«ã¯ä½ããã¢ãµã¼ããã¾ãã
ã¤ã³ã¿ã©ã¯ã·ã§ã³ãã¹ãã§ã¯æ å ±ã®åå¾ã¨åå¾ããæ å ±ã®ã¢ãµã¼ã·ã§ã³ã«é¢ä¿ãã§ãã¾ãã
å¼ã³åºããããã©ãããBoolã§ç¨æããã¨æãã¾ããããã«ã¯èª²é¡ãæãã¾ãã
ããããå½¢ã«ããã¨æ å ±ãä¿åãããªããã§ãã
è¦ãã ãã§ã¯ã¡ã½ãããå¼ã³åºããããã¨ãããããã¾ãããããã¯èª²é¡ã«ãªãã¾ãã
è¤æ°å¼ã³åºããã¦ããã¨èª²é¡ã«ãªãâ
ãã®ä»£ããã«ä½åº¦ãã¡ã½ãããå¼ã³åºãããããè¨é²ãã¾ããã
ãã£ããã«ãªãã¡ã¯ã¿ãªã³ã°ã§2åå¼ã°ããããã«ãªã£ããã¨ãæãã¾ãã
å¾ã ãªãã¡ã¯ã¿ãªã³ã°ããéã«ãå½¹å²ãæããã¾ãã
ããã§ãæ å ±ã¯ä¿åããã¾ãããã§ãç´è¿ã®å¼æ°ã®ã»ãããããã°ããã§ãããã
ããã§Lastã¨ãã¦ç´è¿ããç¡ããã¨ãæ示ãã¾ããã
ããã§ãã¹ããåãã¨ãããã¨ã§ãã
ããã§ã¯æ±ºã¾ãäºãæãã¾ããUnitTestã¯ä¸ã¤ããã¢ãµã¼ã·ã§ã³ã¯åºæ¥ãªãã§ããä¸ã¤ã®çå®ããè¿°ã¹ããã¾ããã
ããã§ã¯4ã¤ã®çµæãè¿°ã¹ã¦ãã¾ãã
åç¬ã«ããæ¹ãããã§ãã
ããã§ã¨ã©ã¼ã¡ãã»ã¼ã¸ã«ã©ããªæå³ããããã§ããã
ãã¹ãã§ã¯ãªãHelperã¡ã½ããããã¤ã³ãããã¾ãã ããã¯åé¡ã«ãªãã¾ããç¹ã«äºéå¥ã®ã¨ãããå¼ãã ãããã¨ã
ãã©ã¡ã¼ã¿ã¼ã§å ´æããããã¾ãããã
Failãããã®ã示ãããããã«ãªãã¾ããã
ã§ã2ã3ã§ã¯ãªããã¨ããããããªãã
2ã¯ä½ã§3ã¯ä½ãªã®ãã
ããã§ã¨ã©ã¼ã¡ãã»ã¼ã¸ã使ãããã®ã«ãªãã¾ããã
ã§ã¯ãããã³ã°ã®é åã¯ï¼é çªãå¤ãã¦ããã¹ããéã£ã¦ã»ããã§ãã
ããã¯èããã¹ãã®ä¾ã§ããå£ãã¦ã»ãããªãã¨ãã«ãå£ããããã©ã¸ã£ã¤ã«ãã¹ã
ä»æ¥ã¯Passãã¦ãææ¥ã¯å£ãããããããªãããããã¯ã·ã§ã³ã³ã¼ãã®å¤æ´ã§å£ãããããããªãã
åºæ¥ããªãUnitTestãSaftyãããã«ãã¦èªä¿¡ãæã£ãå¤æ´ãããããããããã¨ç¶ç¶çãªãªãã¡ã¯ã¿ãªã³ã°ãå¯è½ã«ãªãã¾ãã
ããããã¨ã³ã¼ããé²åãç¶ããããã§ãã
ã³ã¼ãã許ãç¯å²ã§ããæã ã¯Agileã§ãããã¾ããã
大åãªéè¦ãªãã®ã«åå¿ãã¦ãããã»ã©éè¦ã§ãªããã¨ã¯ç¡è¦ããããããããã®ã§ãã£ã¦ã»ããããã§ãã
ããã§ã¯ãã¹ãã®èããå¼±ãããã¨æãã¾ãã
éè¦ãªãã¨ã ãã確èªãã¾ãã
ãã©ã¡ã¼ã¿ãå¤ãã¦ããã¾ããBoolãè¿ãã¾ãã
æå¾ ãããå¤ã¨å®éã®å¤ãã¬ãã¼ããã¾ãã
ç°ãªãé åã®ãããã³ã°ãã¹ãã失æããå ´å
ãããã§ãªãæµ·èãåãåã£ããï¼
æªãã¯ãªããè¯ãã¯ãªãã¨ã©ã¼ã¡ãã»ã¼ã¸ã§ãã
ãã¼ã¿éãå¤ããªãã¨ã©ãã§ããããCIã®ãã°ãªãã©ãã§ãããã
ããç´°ããªã¢ã¦ããããããã¦ãããPredicateã ã¨ããã§ãã
ããã§Hamcrest Matchersãããããè©ä¾¡ããPredicateã§ããæå¾ å¤ã説æãã¦ããã¾ãããã¹ãããããã£ãããMatcherãç´°ãããã®æå¾ å¤ã¨ä½ãéããã説æãã¦ããã¾ãã
MatcherããMatcherãä½ããã¨ãåºæ¥ã¾ãã
使ãããããã«ãã¼ãä½ã£ã¦ã¿ã¾ããã
Stringé åãããã£ã¼ã«ãªãã¾ãã
ãããã³ã°ãchashuã«å¤ãã¦ã¿ã¾ããã
æå¾ å¤ã説æããã¦ãã¾ããã©ã®ãããªé ã§ããããã¨åµã®ã¯è¡ã£ããã®ãã»ããã¨æ¸ãã¦ããã¾ããç´°ãããã¹ãããã®å 容ã説æãã¦ãããè¥ããªã®ã«ãã£ã¼ã·ã¥ã¼ã欲ããã£ãã¨ã
UnitTestã®å¤±æããååãªæ å ±ãå¾ããã¨ãã§ããã°ããã¹ãã¡ãã»ã¼ã¸ããä½ãæªãã£ãã®ããããã¾ãã
å ¨é¨ã«Matcherã使ã£ãã
EqualToã¨ãããããã£ã¼ã使ãã
ã¢ãã¯ãªãã¸ã§ã¯ãã§ã¯ã¤ã³ã¼ã«ã«å¶éããããã¨ã¯ããã¾ããã
丼ã®æ°ãï¼ããããã大ãããã¨ãåºæ¥ã¾ãã
æå¾ã®äºã¤ãç¹å®ããå¿ è¦ã¯ãªãã
ä»ã®Matcherã使ããã¨ãåºæ¥ããPrefix確èªãããã
Fakeãªãã¸ã§ã¯ãã¯å®éã®ãªãã¸ã§ã¯ããé«ä¾¡ãé ããä¿¡é ¼ã§ããªããã°ãã¼ãã«ã¹ãã¼ããå¤ãã¦ãã¾ãæã«ä½¿ãæ©ä¼ãããã¾ãã
å®éæã£ãéãã«å¼ã°ãããã©ããã確èªããæã«ä½¿ãããã®ã«ãªãã¾ãã
ããååãªã¢ãã¯ãªãã¸ã§ã¯ããèãã¾ããã
Boolã§å¼ã°ããããé£ããã«ã«ã¦ã³ããã¾ãããè¡æ°ããã¹ãã¾ãããã¡ãã»ã¼ã¸ãå ãã¾ãããEqualãPredicateãã¤ããæè»æ§ãé«ãèãã軽æ¸ãã¾ããHamcraster Matcherã使ããã¨ãèãã¦ä¸ããã
ç®çã¯ãã¹ããè¡ãã ãã§ã¯ãªãããªãã¡ã¯ã¿ãªã³ã°ããã¦ããã¨ã§ãã
éè¦ãªãã®ã«åå¿ããããã§ãªããã®ã¯ç¡è¦ãããã®ãä½ããããã¬ã©ã¹ã®ããã«ç¹ç´°ãªãã¹ãã¯å¿ è¦ããã¾ããããã±ã®ããã«å¼·ãããªãããªãã¹ããã»ããããã§ãã