Ruby on Railsã®ãã¹ãã®æ¸ãæ¹ ï¼ã¢ãã«ã®åä½ãã¹ãã¨ï¼ã³ã³ããã¼ã©ã®æ©è½ãã¹ãï¼
Ruby on Rails ã®ãã¹ãã®æ¸ãæ¹ã®ã¾ã¨ãã
RSpecã使ããªãï¼ç´ ã®
- unit test (ã¢ãã«ã®ãã¹ã)
- functional testï¼ã³ã³ããã¼ã©ï¼ãã¥ã¼ã®ãã¹ãï¼
ã«ã¤ãã¦ï¼ã©ãæ¸ãããããã®ãï¼ã©ãã®æ å ±ãåç §ãããããã®ãï¼ãªã©ãåæã
Railså ¥éè ãåãã¦ãã¹ããæ¸ãéã«è¿·ãããªãããã«ããããã®ã¬ã¤ãã©ã¤ã³å½¢å¼ã
Railsã®ãã¼ã¸ã§ã³ã¯1.2ç³»ãæ³å®ã
ç·èª¬
ä¸åº¦ã¯ç®ãéãã¹ãï¼å ¥éç¨ã®ãªã³ã¯éï¼
RubyOnRails ã使ã£ã¦ã¿ã ã第 6 åã ãã¹ãã®æ¸ãæ¹
http://jp.rubyist.net/magazine/?0013-RubyOnRailsRailsã§ãã¹ããæ¸ãåæ
http://d.hatena.ne.jp/moro/20061029/1162116778Railsã§ã®ãã¹ã
http://d.hatena.ne.jp/akm/20061126/1164552662
å¹³æãªãµã³ãã«ã³ã¼ãï¼
Ruby on Railsã§TestãããããâãRuby on Rails 㧠units test ã®ä»æ¹
http://maskana.homedns.org/rails/pro/detail/test/48Ruby on Railsã§TestãããããâãRuby on Rails 㧠functionals test ã®ä»æ¹
http://maskana.homedns.org/rails/pro/detail/test/49
ï¼ï¼ï¼ã¢ãã«ã®åä½ãã¹ã
決ã¾ãï¼ãã¡ã¤ã«ã®å ´æãªã©ï¼ï¼
ãã¹ãã³ã¼ãï¼
- /test/unit/ã¢ãã«å(åæ°å½¢)_test.rb
- ãã¹ãã¡ã½ããã®å称㯠test_ ã§å§ãã
ãã¹ããã¼ã¿ï¼ãã£ã¯ã¹ãã£ï¼ï¼
- /test/fixtures/ãã¼ãã«å(è¤æ°å½¢)_test.rb
- åã ã®ãã¹ãã¡ã½ããã®å®è¡ç´åã«ï¼æ¯åãã®ãã¼ã¿ã ããæå ¥ããã
- ãã£ã¯ã¹ãã£å ã®åã ã®æå ¥ã¬ã³ã¼ãã«ã¯ï¼ååï¼ãã£ã¯ã¹ãã£åï¼ãã¤ãããã
ãã¹ãå®è¡æ¹æ³ï¼
- rake test:units ã§å ¨å®è¡
- ruby test/unit/ãã¡ã¤ã«å ã§åå¥ã«å®è¡
ãã¹ãã¡ã½ããã®ä½ææ¹éï¼
- DBã«å¯¾ãã¦ï¼æå¾ éãã®æä½ãè¡ãªãã¦ãããã©ãããæ¤è¨¼ããã
- ã¢ãã«ã®å ¨ã¡ã½ãããç¶²ç¾ ãããã¢ãã«ã®1ã¡ã½ããã«å¯¾ãã¦ï¼æä½1ã¤ä»¥ä¸ã®ãã¹ãã¡ã½ãããæ¸ãã
- Railsã§ã¯ã¢ãã«ã«ãã¸ãã¹ãã¸ãã¯ãè©°ãè¾¼ã¾ãï¼è¦ã¨ãªãã®ã§ã
- ã¢ãã«ã®1ã¡ã½ãããï¼å¤§ããç°ãªã£ãè¤æ°ã®æ¯ãèããããå ´åï¼ããããã®æ¯ãèãã«å¯¾ãã¦åå¥ã«ãã¹ãã¡ã½ãããæ¸ãã
- â»ãããï¼ããã ã£ããã¢ãã«ã®1ã¡ã½ããããªãã¡ã¯ã¿ãªã³ã°ãã¦è¤æ°ã¡ã½ããã«åå²ããã»ããããã ãããã©ãããåºæºã§ãã¹ãã¡ã½ãããåå²ãã¦ããã®ãï¼ä½è¨ãªèª¬æãå¿ è¦ã«ãªã£ã¦ãã¾ãããã
- ã¢ãã«ã¡ã½ãã以å¤ã«ï¼ã¢ãã«ã®ããªãã¼ã·ã§ã³ã«ã¼ã«ãªã©ã®åã ã®æ§è³ªã»æåããã¹ãã³ã¼ãä½æ対象ã«ãã¦ããã
å
·ä½çãªãã¹ãå
容ï¼
Test::Unitã使ããï¼Ruby on Railsã«ç¹åãããã¹ãæ¹æ³ã§ã¯ãªãï¼
- assertç³»ã®ã¡ã½ããä¸è¦§ï¼
- assert( hoge ) : hogeãtrueã§ãããã¨ãè¦æ±
- assert_equal( expected, actual ) : expected == actualã§ãããã¨ãè¦æ±
- assert_not_equal( expected, actual ) : expected != actualã§ãããã¨ãè¦æ±
- flunk( ã¡ãã»ã¼ã¸ ) : 常ã«å¤±æ
- ã»ãã®ãã¹ãã¡ã½ããã¯ä¸è¨ã®å¿ç¨ã§æ¸ã¾ããäºãå¯è½
ãããã¹ã対象ãåç §ç³»ã®ã¡ã½ããã§ããã°ï¼ãã¹ãã¡ã½ããä¸ã§ã¯ï¼
- ãã®ã¡ã½ããã使ã£ã¦ï¼ç¾å¨ã®DBå 容ãèªã¿è¾¼ãã
- (1)ã®è¿ãå¤ãï¼æå¾ éãã®æ§é ï¼å 容ãã©ãããassertã§æ¤è¨¼ã
- Fixtureã«æ¸ãã¦ããéãã®å 容ãã
ãããã¹ã対象ãæ´æ°ç³»ã®ã¡ã½ããã§ããã°ï¼ãã¹ãã¡ã½ããä¸ã§ã¯ï¼
- ãã®ã¡ã½ããã使ã£ã¦ï¼DBã®ç¶æ ãå¤æ´ããã
- åç §ç³»ã®ã¡ã½ããï¼â»find(:all)ã®ãããªåºæ¬çãªã¡ã½ããï¼ã使ã£ã¦ï¼ç¾å¨ã®DBå 容ãèªã¿è¾¼ãã
- (2)ã®è¿ãå¤ãï¼æå¾ éãã®æ§é ï¼å 容ãã©ãããassertã§æ¤è¨¼ã
- Fixtureã«å¯¾ãã¦ï¼(1)ã®æä½ãå ãããããããªå 容ãã
ãããã¹ã対象ããã®ä»ã®ã¢ãã«ã®æ§è³ªã§ããã°ï¼ãã¹ãã¡ã½ããä¸ã§ã¯ï¼
- newã§ã¢ãã«ã®ã¤ã³ã¹ã¿ã³ã¹ãä½æããã
- (1)ã®ã¢ãã«ãªãã¸ã§ã¯ããã®ãã®ãæä½ãããï¼ã¢ã½ã·ã¨ã¼ã·ã§ã³è¨å®ãªã©ï¼
- (2)ã®å¾ã«ï¼ã¢ãã«ãªãã¸ã§ã¯ããæå¾ éãã®æ§è³ªãæã£ã¦ãããã©ãããassertã§æ¤è¨¼ã
ç¹çäºé
ï¼
- setup()å ã«å ±éã®äºåå¦çãæ¸ããã
- ãã¼ãã«å( :ãã£ã¯ã¹ãã£å ) ã§ï¼ãã£ã¯ã¹ãã£ãã¼ã¿å ã®ç¹å®ã®ã¬ã³ã¼ããActiveRecordã¨ãã¦åç §ã§ããã
ï¼ï¼ï¼ã³ã³ããã¼ã©ã®æ©è½ãã¹ã
決ã¾ãï¼ãã¡ã¤ã«ã®å ´æãªã©ï¼ï¼
ãã¹ãã³ã¼ãï¼
- /test/functional/ã³ã³ããã¼ã©å_test.rb
- ãã¹ãã¡ã½ããã®å称㯠test_ ã§å§ããï¼ã¢ãã«ã¨åãï¼
ãã¹ããã¼ã¿ï¼fixtureï¼ï¼
- ã¢ãã«ã¨åã
ãã¹ãå®è¡æ¹æ³ï¼
- rake test:functionals ã§å ¨å®è¡
- ruby test/functional/ãã¡ã¤ã«å ã§åå¥ã«å®è¡
ãã¹ãã¡ã½ããã®ä½ææ¹éï¼
- ãªã¯ã¨ã¹ãã«å¯¾ãã¦ï¼æå¾ éãã®ã¬ã¹ãã³ã¹ãè¿ã£ã¦ããããæ¤è¨¼ããã
- publicãªå ¨ã¢ã¯ã·ã§ã³ãç¶²ç¾ ããã
- ã³ã³ããã¼ã©ã¨ãã¥ã¼ããã¹ã対象ã
ãã¹ãã®ããªã¥ã¼ã ï¼
- å¢ããã¹ãç¹ï¼
- ã¯ã©ã¤ã¢ã³ãã³ã¼ãï¼JavaScriptï¼ã絡ããããªæ§è³ªã®ãã¹ãã¯ï¼Rubyå´ã§ã¯ãªãï¼Seleniumã«ä»»ããå¿ è¦ãããã*1
- ã¨ã¯è¨ã£ã¦ãï¼Seleniumã®ãã¹ãã¯å®è¡ã«æéãããã£ã¦ãã¾ãã
- Seleniumã®ãã¹ãã±ã¼ã¹ãè¨ãä¸ãããªãããã«ï¼JavaScriptã絡ã¾ãªãé¨åã¯Railså´ã®ã³ã³ããã¼ã©ãã¹ããå¢ãããã¨ã«ãã£ã¦å¸åãã¦ããã
- å¢ããã¹ãã§ãªãç¹ï¼
- ç»é¢ã®è©³ç´°ãªã¬ã¤ã¢ã¦ãã¯ï¼éçºã«ããã¦æãå¤æ´ãããããç¹ãªã®ã§ï¼ãã¹ãé ç®ã«å«ãã¦ã¯ãªããªããï¼ãå¤æ´ã«å¼·ããããããã®ãã¹ããªã®ã«ï¼ããã§ã¯éã«ï¼ãã¹ãã®ããã§å¤æ´ã«å¼±ããªã£ã¦ãã¾ããï¼
- DOMè¦ç´ ãåå¨ãããã©ããã®å¤å®ãããã«çãã¦ããã
å
·ä½çãªãã¹ãå
容ï¼
ãªã¯ã¨ã¹ãçæ(get/postãªã©)ã¯ï¼ActionController::TestProcessã¢ã¸ã¥ã¼ã«å ã®ã¡ã½ãããå©ç¨ããã
- http://api.rubyonrails.org/classes/ActionController/TestProcess.html
- http://apidock.com/rails/ActionController/TestProcess
æ¤è¨¼ã¯ï¼ActionController::Assertionsã¢ã¸ã¥ã¼ã«å ã®ã¡ã½ãããå©ç¨ããã
- http://api.rubyonrails.org/classes/ActionController/Assertions.html
- http://apidock.com/rails/Test/Unit/Assertions
ãµã³ãã«ãªã©ï¼
- http://abekatsu.blogspot.com/2009/10/rails-functional-test-1.html
- http://abekatsu.blogspot.com/2009/10/rails-functional-test-2.html
- http://d.hatena.ne.jp/elm200/20070724/1185282738
æé ï¼
- (1)ãªã¯ã¨ã¹ããçæã»å®è¡ããã
- (1-1)ã¾ãï¼HTTPãã©ã¡ã¼ã¿ãã»ãã·ã§ã³ãã¼ã¿ãæºåã
- (1-2)ãããã®ãã©ã¡ã¼ã¿ã使ã£ã¦ï¼ã¢ã¯ã·ã§ã³ã¸ã®HTTPãªã¯ã¨ã¹ããã·ãã¥ã¬ã¼ãããã
- get :ã¢ã¯ã·ã§ã³å, ãã©ã¡ã¼ã¿, ã»ãã·ã§ã³
- post :ã¢ã¯ã·ã§ã³å, ãã©ã¡ã¼ã¿, ã»ãã·ã§ã³
- xhr :ãªã¯ã¨ã¹ãã¡ã½ãã, :ã¢ã¯ã·ã§ã³å, ãã©ã¡ã¼ã¿, ã»ãã·ã§ã³
- (2)ï¼ãªã¯ã¨ã¹ãã®çµæã¨ãã¦Railsãè¿å´ãã¦ããï¼ã¬ã¹ãã³ã¹ãæ¤è¨¼ããã
- HTTP status⦠assert_response :success ããã㯠:redirect
- redirectå ⦠assert_redirected_to :controller => ã³ã³ããã¼ã©å, :action => ã¢ã¯ã·ã§ã³å
- DOMè¦ç´ ã®åå¨å¤å®â¦ assert_tag :tag => "span", :attributes => { :id => "hoge" }
- DOMè¦ç´ ã®éåå¨å¤å®â¦ assert_no_tag :tag => "span", :attributes => { :id => "hoge" }
- (3)ã³ã³ããã¼ã©ã®äºå¾ç¶æ ãæ¤è¨¼ããã
- ãã¥ã¼ãã³ãã¬ã¼ãã«æ¸¡ãããã¤ã³ã¹ã¿ã³ã¹å¤æ°ï¼@hogeï¼ãæå¾ éãã®å¤ãã©ãããassertã§æ¤è¨¼ã
- ã¤ã³ã¹ã¿ã³ã¹å¤æ°ã¯ assigns( å¤æ°å ) ã§åã£ã¦ãããã
- (4)å¿ è¦ã«å¿ãã¦ï¼DBã®äºå¾ç¶æ ãæ¤è¨¼ããã
- ã¢ãã«ã®åç §ç³»ã¡ã½ãããå¼ã³åºãã¦ï¼ã³ã³ããã¼ã©ã®ã¢ã¯ã·ã§ã³ãæå¾ éãã®DBæä½ãè¡ãªã£ããã©ãããassertã§æ¤è¨¼ãhttp://www.thinkit.co.jp/cert/article/0605/2/4/4.htm
- ãã ãï¼ã³ã³ããã¼ã©å±¤ã®ãã¹ãã«ï¼ã¢ãã«å±¤ã®ã¡ã½ããå¼ã³åºããæ··å¨ããã¦ãããã©ããã¯ååèãããã¨ã
ï¼ï¼ï¼ãã®ä»ï¼å ±éäºé
- å
¨ãã¹ãã®ä¸æå®è¡
- rake test
- ãã¹ãã®å¿
è¦æ§ï¼
- ã³ã³ãã¤ã«ãç¡ãã®ã§ï¼ããã¦å ¨è¡ãéãã¦å®è¡ãã¹ãã
- æåããï¼ãã¹ãããããããã«ã³ã¼ããæ¸ãã
- è¯ãè¨è¨ã¨ã¯ããã¹ãããããããã¨ã§ãã
http://blogs.itmedia.co.jp/hiranabe/2005/08/post_e66c.html - ãã¹ãããããMVCã®é åã«ã¤ãã¦ï¼
- è¯ãè¨è¨ã¨ã¯ããã¹ãããããããã¨ã§ãã
Railsã§ã®ãã¹ã
http://d.hatena.ne.jp/akm/20061126/1164552662
- ã³ã³ããã¼ã©ããã¥ã¼ã§ã¯ãªããã¢ãã«ã§å®è£ ã§ããé¨åã¯ã§ããã ãã¢ãã«ã«æ¸ãã¦ããã¨ããã¹ããæ¸ãã®ã楽ã«ãªã
Ruby on Railsã®ãããMVCãã®å¼å®³
http://satoshi.blogs.com/life/2009/10/rails_mvc.html
- åã«DBãæ½è±¡åããã ãï¼ORMï¼ã ã¨ï¼çã®æå³ã§ã®MVCã§ã¯ãªããããã¸ãã¹ãã¸ãã¯ã®ã«ãã»ã«ãã¨ããæå³ã§ã¢ãã«å±¤ãæ¸ããã¨ã
- ã¢ãã«å±¤ã¯ï¼ãã¼ã¿ã®æ´åæ§ï¼ãã©ã³ã¶ã¯ã·ã§ã³ï¼ã®è²¬ä»»ãï¼ï¼ï¼ï¼ è² ããã¨ãè¤æ°ã¢ãã«ã®ã¡ã½ãããã³ã³ããã¼ã©ãããã©ãã©ã«å¼ã³åºãã¦ãã®ãã§ãããããªç¶æ³ã¯NGãDAOã®æå³ã§ã®ã¢ãã«ã¯ã©ã¹éã®ä¸ã«ä¸ç®ãã¶ããï¼è¦ªçã®ã¢ãã«ã¯ã©ã¹ãå¿ è¦ãï¼è¦ªã¢ãã«/åã¢ãã«ã«åããï¼
ããMVCã«ã¤ãã¦ããããä¸è¨è¨ã£ã¦ããã
http://d.hatena.ne.jp/higayasuo/20091013/1255408723
- ã¢ãã«ã¯åããã¹ãã ãï¼åããããææ¡ãã¥ãããç¹å®ã®ã¦ã¼ã¹ã±ã¼ã¹ã§ãã使ãããªãã¡ã½ããã¯ï¼ã¢ãã«ã§ã¯ãªãç¹å®ã®ã³ã³ããã¼ã©ã«ç½®ãã¦ãã£ã¦ãããããã®è¾ºã¯ãã©ã³ã¹ãåãã¹ãã
- ä¸ã®ä¸ã«ã¢ãã«ãåããããã風潮ãããåå ã¯ï¼ä¸æåã¾ã§ã³ã³ããã¼ã©ããã¹ããã¥ããã£ãããã
ãªãä¸è¨ã®ååã¯Railsã«éããï¼ä»ã®è¨èªããã¬ã¼ã ã¯ã¼ã¯ã§ãåãã§ããã
Web ã¢ããªã® MVC è¨è¨ã¾ã¨ãï¼CakePHPï¼
http://d.hatena.ne.jp/p4life/20091014/12555326181ãã¼ãã«ã1ã¢ãã«ã«å¯¾å¿ãã¦ãããã¬ã¼ã ã¯ã¼ã¯ã§ã¯â¦ãã©ã³ã¶ã¯ã·ã§ã³ããã¸ãã¹ãã¸ãã¯ãã³ã³ããã¼ã©ã«æ··ããè¾¼ã¾ãªãããã«æ³¨æã
ããããã®ã¢ãã«åä½ãã©ãã ããã¹ãããã¨ãã¦ããã³ã³ããã¼ã©ã®ãã¹ããããªããã°ã¢ããªã±ã¼ã·ã§ã³ã®ä¿¡é ¼æ§ã¯ä¸ããã¾ããã対çã¨ãã¦ãã¢ãã«ã«åã¾ããæªããã¸ãã¹ãã¸ãã¯ã¯ Service ã¨ããã¢ãã«ã®å½¢æ ã«ç½®ããã¨ãããããããã¦ãã¾ã
ã
*1:Exceleniumã使ãã¨ããã ããã