ã¤ã³ãããã¯ã·ã§ã³
2013/08/14
é£è¼ç¬¬1åç®ã¯ãRuby on Railsã®ãã¹ãã«é¢é£ããåºæ¬çãªæ¦å¿µã¨ãã¼ã«ã«ã¤ãã¦æ¸ãã¾ãã
ããããããã¹ããã¨ã¯
æ¥å¸¸çæ´»ã§ããã¹ããã¨è¨ãã°ãããã¦ãã¯å¦æ ¡ã塾ã®å¦å試é¨ãæå³ãã¾ããã½ããã¦ã§ã¢éçºã®æèã§ã¯ãããã¹ããã¯ç¬¬ä¸ç¾©çã«ã¯ã½ããã¦ã§ã¢ãæ£ããåä½ãã¦ãããã©ããã確èªããä½æ¥ãæãã¾ãã
ããããã½ããã¦ã§ã¢ã®éçºè ãã¡ã¯ããã¹ããæ¸ããã¨ããè¨ãæ¹ãé »ç¹ã«ä½¿ç¨ãã¾ãããã®å ´åã®ãã¹ãã¯ããããã½ããã¦ã§ã¢ã®ãã¹ããè¡ãããã°ã©ã ãã¨ããæå³ã§ãã
ä¾ãã°ãããªãã®ä¼ç¤¾ãä¼è¨ã·ã¹ãã ã®éçºãåè¨ããã¨ãã¾ããç´ååã«ã¯äººéãå®éã«æä½ãã¦ãã®ä¼è¨ã·ã¹ãã ãä»æ§éãã«åããã©ãããã§ãã¯ãã¾ããããã®ä½æ¥ãæ¬æ¥ã®æå³ã§ã®ããã¹ããã§ããããããéçºä¸ã«ãä¼è¨ã·ã¹ãã ã®å ¨ä½ãããã¯ä¸é¨ããã¹ãããªããã°ãªãã¾ããããã®ä½æ¥ã¯ç¹°ãè¿ãè¡ãããã®ã§ãå½ç¶ãªããèªååããããªãã¾ããããã§éçºè ãã¡ã¯ããã¹ãããæ¸ãã¾ããããªãã¡ããã¹ãã¨ããååã®ããã°ã©ã ãä½ãã¾ãã
ãã®é£è¼ã§ããã¹ããã¨ããè¨èã¯ãã»ã¨ãã©å¾è ã®æå³ã§ä½¿ç¨ããããã¨ã«ãªãã¾ãã
ãã¹ãé§åéçº(TDD)ã¨ããã¤ãã¢é§åéçº(BDD)
ãã¹ãé§åéçºï¼test-driven development; TDDï¼ã¯21ä¸ç´ã«ãªã£ã¦åºãæ®åããã½ããã¦ã§ã¢éçºææ³ã§ããä¼çµ±çãªã½ããã¦ã§ã¢éçºã§ã¯ãã½ããã¦ã§ã¢ã®å ¨ä½ãããã¯ä¸é¨ãã§ãããã£ã¦ããæåã§ãã¹ããããããã¹ããæ¸ããããã¦ãã¾ãããããã¹ããæ¸ãã¦ããã½ããã¦ã§ã¢ãä½ããã¨ãã風ã«æé ãéã«ãã人ã ãç¾ãã¾ããããã®æ £è¡ã«ã¯éçºå¹çãä¸ããå¾åããã£ãã®ã§ãããã¹ãé§åéçºãã¨ããååãä¸ãããã¦çè«åããã¾ããã
ããã¤ãã¢é§åéçºï¼behavior-driven development; BDDï¼ã¯ããã¹ãé§åéçºããæ´¾çããã½ããã¦ã§ã¢éçºææ³ã§ããæ¬é£è¼ã®ã¡ã¤ã³ãã¼ãã§ããRSpecã¯ãã¾ãã«BDDãå®è·µããããã®ãã¼ã«ã§ãã
BDDã«å ·ä½çãªç¹å¾´ã«ã¤ãã¦ã¯é£è¼ã®ä¸ã§å°ããã¤è§¦ãã¦ããã¾ãããè¦ç¹ã¯ãã½ããã¦ã§ã¢ã®æ¯ãèãï¼behaviorï¼ãèªç¶è¨èªï¼è±èªãæ¥æ¬èªï¼ã§è¨è¿°ãããã¨ãããã¨ã§ããã½ããã¦ã§ã¢ã®æ¯ãèãã¯ãä»æ§ï¼specificationï¼ãã¨è¨ãæãããã¨ãã§ãã¾ããä»æ§ãã³ã¡ã³ãã®å½¢ã§ã½ã¼ã¹ã³ã¼ãä¸ã«æ®ããã¨ã¯ãåä¸ç´ããè¡ããã¦ããæ £ç¿ã§ããããããä»æ§ããã¹ãï¼ããã°ã©ã ï¼ã®ã¡ã½ããåãããã¯ã¡ã½ããã®å¼æ°ã¨ãã¦è¡¨ç¾ã§ããããã«ããç¹ãæ°ããã®ã§ãã
RSpec
Rubyã®ä¸çã«ã¯ã主è¦ãª3ã¤ã®ãã¹ããã¬ã¼ã ã¯ã¼ã¯ãåå¨ãã¾ããTest::UnitãMiniTestãRSpecã§ãã
Test::Unitã¯Ruby 1.8ã®ãMiniTestã¯Ruby 1.9/2.0ã®ã¹ã¿ã³ãã¼ãã©ã¤ãã©ãªã§ããMiniTestã¯Test::Unitã¨äºææ§ãä¿ã¡ã¤ã¤ãBDDã®è¦ç´ ãåãå ¥ãããã®ã§ãããã§ã«Ruby 1.8ã¯éå»ã®ãã®ã«ãªãã¾ããã®ã§ãç¾å¨ã§ã¯MiniTestã¨RSpecãäºå¤§å¢åã§ããã¨è¨ã£ã¦ããã§ãããã
RSpecã¯2005å¹´8æã«æåã®ãã¼ã¸ã§ã³(0.1.0)ãå ¬éããã¾ããããã§ã«8å¹´ã®æ´å²ãæã¡ã¾ããè§è°·æ°ã¨è«¸æ©æ°ã«ãããã¹ã¯ã¹ããã¯ã®ã¹ãã¨ããç´¹ä»è¨äºãRubyist Magazineã«æ²è¼ãããã®ã¯2007å¹´ã®ãã¨ã§ããããã®æç¹ã§ã®ãã¼ã¸ã§ã³ã¯1.0.8ãæ¬ç¨¿å·çæ(2013å¹´8æ14æ¥)ã«ãããææ°çã¯ã2.14.1ã§ããRSpecã®æ ¹æ¬çãªèãæ¹ã«å¤åã¯ãªãããããã¾ããããAPIã¬ãã«ã§ã¯ç¸å½ãªæ°ã®å¤æ´ç¹ãããã¾ãã
RSpecã®ã³ã¢ãã¼ã ã¯ãã§ã«RSpec 3ã«åãã¦åãåºãã¦ãããå æã«ã¯Myron Marstonæ°ã«ããRSpec 3ã«åãã¦ã®è¨ç»(æ¥æ¬èªè¨³)ãçºè¡¨ããã¦ãã¾ãã
æ¬é£è¼ã§ã¯ããã®ãããªå¤åãæèãã¤ã¤è©±ãé²ãã¾ãã
Capybara
GitHub ãªãã¸ããªã®ã¿ã¤ãã«ã«ããã°ãCapybaraã¯åãå ¥ããã¹ãï¼acceptance testï¼ã®ãã¬ã¼ã ã¯ã¼ã¯ã§ããæ¬æ¥ã®ãåãå ¥ããã¹ããã¨ã¯ã·ã¹ãã ã®çºæ³¨è ãç´åç©ãä»æ§ã«åè´ãã¦ãããã©ããã確èªããä½æ¥ãæå³ãã¾ãããããã§ã¯å¥ã®æå³ã§ä½¿ç¨ããã¦ãã¾ãã
Capybaraãå©ç¨ããã¨ãéçºè ã¯Webãã©ã¦ã¶ã«ããã·ã¹ãã ã¸ã®ã¢ã¯ã»ã¹ãã·ãã¥ã¬ã¼ãã§ãã¾ããä¾ãã°ã人ãWebãã¼ã¸ä¸ã®ãã¦ã¼ã¶ã¼åãã¨ããã©ãã«ãæã¤ããã¹ããã£ã¼ã«ãã«ãtaroãã¨å ¥åããããã¹ã¯ã¼ããã¨ããã©ãã«ãæã¤ãã¹ã¯ã¼ããã£ã¼ã«ãã«ãpasswordãã¨å ¥åãã¦ãããã°ã¤ã³ããã¿ã³ãã¯ãªãã¯ããå ´åã«ä½ãèµ·ãããããã§ãã¯ã§ãã¾ãããããCapybaraã®è¨ãã¨ããã®ãåãå ¥ããã¹ããã§ãã
Capybaraã®æåã®ãã¼ã¸ã§ã³(0.1)ããªãªã¼ã¹ãããã®ã¯ã2009å¹´11æã®ãã¨ã§ããæ¬ç¨¿å·çæ(2013å¹´8æ14æ¥)ã«ãããææ°çã¯ã2.1.0ã§ãã
次åã¯
次åã®æ²è¼ã¯ãã¤ã«ãªããåããã¾ããããRails 4.0ã®ã¢ããªã±ã¼ã·ã§ã³ãæ°è¦ä½æãã¦ãRSpecã¨Capybaraãå°å ¥ãããHello Worldãçãªãã¹ããæ¸ãã¦ããã¹ããå®è¡ããã¨ããã¾ã§é²ã¿ããã¨èãã¦ãã¾ããã§ã¯ãã¾ãã