ãã®ã³ã¼ãã¯ãã¹ãããã¦ãããï¼
ãã¹ãã³ã¼ããæ¸ãã¦ããã¨ããµã¨çåãæãã¦å¿é ã«ãªããã¨ãããã
- æããã¦ããã®ãã¹ãã³ã¼ãã§æ¼ããªãåä½ç¢ºèªã§ãã¦ããã®ã ãããï¼
- ãã¹ããæ¼ãã¦ããã¨ããããã©ããã«ç¡ãã ãããï¼
ç¹ã«ãã¹ããæ¸ããã¨ã«æ £ãã¦ããªãç¾ç¶ã§ã¯ãä½å¦ã§ã©ããªãã¹ããæ¸ãã¦ããã¹ãããææ¢ãã ããããªærcovãåæ°ã¥ãã¦ãããã
ã¤ã³ã¹ãã¼ã«
- ã¤ã³ã¹ãã¼ã«ã¯ãã¤ããªãããã¨ã¦ãç°¡åã
$ sudo gem install rcov
使ãæ¹
- éçºä¸ã®Railsããã¸ã§ã¯ãã®ã«ã¼ãã§ä»¥ä¸ã®ã³ãã³ããå®è¡ãã¦ã¿ãã
- -x Library/Ruby/Gemsãªãã·ã§ã³ã¯ãLibrary/Ruby/Gems/以ä¸ã®ã³ã¼ãã«å¯¾ããcoverageãé¤å¤ãã¦ãããã
- --railsãªãã·ã§ã³ã¯ãconfig/, environment/, vendor/以ä¸ã®ã³ã¼ãã«å¯¾ããcoverageãé¤å¤ãã¦ãããã
- test/**/*_test.rbã«ãã£ã¦ãtestãã©ã«ã以ä¸ã®oooo_test.rbã¨ããæ¸å¼ã®ãã¡ã¤ã«ãã¹ã¦ã調æ»å¯¾è±¡ã®ãã¹ãã«ãªãã
$ cd ~/railsapp/test_slip202 $ rcov -x Library/Ruby/Gems --rails test/**/*_test.rb
- ã³ãã³ããå®äºããã¨ãtest_slip202/coverageãã©ã«ã以ä¸ã«HTMLãã¡ã¤ã«ãçæããã¦ãããindex.htmlãéãã¦ã¿ãã
- ä¸çªä¸ã®è¡app/models/slip.rbã®Code coverageãè¦ãã¨ã61.2%ãã¨è¡¨ç¤ºããã¦ãããã¤ã¾ããå®è¡ã³ã¼ãã®ãã¡ã61.2%ãã¯ä½ããã®ãã¹ããããã¦ããããæ®ãã®ã38.8%ãã¯å ¨ããã¹ãããã¦ããªãã¨ãããã¨ã ã
- app/models/slip.rbã®ãªã³ã¯ãã¯ãªãã¯ããã¨ãã³ã¼ãã®è©³ç´°ã確èªã§ããã
- ãã¹ãããã¦ããªãã³ã¼ãã«ã¤ãã¦ã¯ã赤ã§ç¤ºããã¦ããã確èªãã¦ã¿ãã¨ãæ¿å ¥ã»åé¤ã»ã³ãã¼é¢é£ã®ã¡ã½ããã赤ããªã£ã¦ããã確ãã«ãã¹ãã³ã¼ããã¾ã æ¸ãã¦ããªãã£ã...ã
- ç¡é§ãå¤ãæ°ããããã以ä¸ã®ããã«ãã¹ãã追å ãã¦ã¿ãã
# ãã¹ã: test/units/slip_test.rb require File.dirname(__FILE__) + '/../test_helper' class SlipTest < ActiveSupport::TestCase # Replace this with your real tests. def test_truth assert true end def test_invalid_with_empty_attributes slip = Slip.new journals = slip.make_journals("j123456789"=>{}) assert !slip.valid? assert slip.errors.invalid?(:number) assert slip.errors.invalid?(:executed_on) assert slip.errors.invalid?(:total_yen) assert slip.errors.invalid?(:base) end def test_invalid_with_not_equal_total_yen slip = Slip.new(:number=>"1", :executed_on=>"2/1", :total_yen=>"2000") journals = slip.make_journals("j123456781"=>{:comment=>"test", :yen=>"1000"}) assert !slip.valid? assert_equal "Total yenãæç´°ã®åè¨ã¨ä¸è´ãã¦ãã¾ããã", slip.errors.on(:total_yen) end def test_copy_journal slip = Slip.new(:number=>"1", :executed_on=>"2/1", :total_yen=>"1000") journals = slip.make_journals("j123456789"=>{:comment=>"test", :yen=>"1000", :position=>1, :index=>"j123456789"}) journals_count = slip.editing_journals.size original_journal = journals[0] copy_journal = slip.copy_journal("j123456789") assert_equal slip.editing_journals.size, journals_count + 1 assert_equal original_journal.comment, copy_journal.comment assert_equal original_journal.yen, copy_journal.yen assert_equal slip.editing_journals[0].index, "j123456789" assert_not_equal slip.editing_journals[1].index, "j123456789" end def test_insert_journal slip = Slip.new(:number=>"1", :executed_on=>"2/1", :total_yen=>"1000") journals = slip.make_journals("j123456789"=>{:comment=>"test", :yen=>"1000", :position=>1, :index=>"j123456789"}) journals_count = slip.editing_journals.size original_journal = journals[0] insert_journal = slip.insert_journal("j123456789") assert_equal slip.editing_journals.size, journals_count + 1 assert_nil insert_journal.comment assert_nil insert_journal.yen assert_not_equal slip.editing_journals[0].index, "j123456789" assert_equal slip.editing_journals[1].index, "j123456789" end def test_delete_journal slip = Slip.new(:number=>"1", :executed_on=>"2/1", :total_yen=>"3000") journals = slip.make_journals("j123456781"=>{:comment=>"test", :yen=>"1000", :position=>1, :index=>"j123456781"}, "j123456782"=>{:comment=>"test", :yen=>"2000", :position=>2, :index=>"j123456782"}) journals_count = slip.editing_journals.size delete_journal = slip.delete_journal("j123456782") assert_equal slip.editing_journals.size, journals_count - 1 assert_nil slip.journal_at("j123456782") end end
- åã³ãrcovãå®è¡ãã¦ã¿ãã
$ rcov -x Library/Ruby/Gems --rails test/**/*_test.rb
ãã©ã¦ã¶ã§coverage/index.htmlãåèªã¿è¾¼ã¿ãã¦ã¿ãã¨...app/models/slip.rbã®Code coverageã¯100%ã«ãªã£ããã²ã¨ã¾ãå®å¿ã§ããï¼
ã³ã³ããã¼ã©ã¼ã®è¿½å ãã¹ã
- åãããã«ã³ã³ããã¼ã©ã¼ã®ãã¹ãã«ãæ¼ãããã£ãã®ã§ã以ä¸ã®ããã«è¿½è¨ãã¦ã¿ãã
- createãupdateãæ¤è¨¼ã¨ã©ã¼ã§ç»é²ã§ããªãã£ãå ´åã®ãã¹ãã追å ãããï¼ãã®ãã¹ãã«ã¤ãã¦ã¯å ¨ãå¿ãã¦ããã®ã§ãrcovã§çºè¦ã§ãã¦è¯ãã£ããï¼
- æç´°è¡ã®æ¿å ¥ã»åé¤ã»ã³ãã¼ã®ãã¹ãã追å ããã
require File.dirname(__FILE__) + '/../test_helper' class SlipsControllerTest < ActionController::TestCase ...ï¼ä¸ç¥ï¼... def test_should_not_create_slip post :create, {:slip =>{:number=>'3', :executed_on=>'2/20', :total_yen=>'3000'}, :journal=>{"1"=>{:yen=>"1000", :index=>"j943792543", :position=>"1", :comment=>"test3"}} } assert_response :success assert_template 'new' end def test_should_not_update_slip put :update, {:id =>'1', :slip =>{:number=>'1', :executed_on=>'2/14', :total_yen=>'10,000'}, :journal=>{"1"=>{:yen=>"1,000", :index=>"j943792544", :position=>"1", :comment=>"test"}} } assert_response :success assert_template 'edit' end def _test_should_insert_row xhr :post, :insert_row, {:index =>'1', :slip =>{:number=>"1", :executed_on=>"2/1", :total_yen=>"1000"}, :journal=>{"1"=>{:comment=>"test", :yen=>"1000", :position=>1, :index=>"1"}} } assert_response :success assert_select 'tbody[id^=j9] tr' do assert_select 'th', 2 assert_select 'td', 2 end assert_select 'tbody#journals_footer tr' do assert_select 'th', 3 assert_select 'th input[value=1,000]', 1 end end def _test_should_copy_row xhr :post, :copy_row, {:index =>'1', :slip =>{:number=>"1", :executed_on=>"2/1", :total_yen=>"1000"}, :journal=>{"1"=>{:comment=>"test", :yen=>"1000", :position=>1, :index=>"1"}} } assert_response :success assert_select 'tbody[id^=j9] tr' do assert_select 'th', 2 assert_select 'td input[value=test]', 1 assert_select 'td input[value=1,000]', 1 end assert_select 'tbody#journals_footer tr' do assert_select 'th', 3 assert_select 'th input[value=2,000]', 1 end end def _test_should_delete_row xhr :post, :delete_row, {:index =>'1', :slip =>{:number=>"1", :executed_on=>"2/1", :total_yen=>"1000"}, :journal=>{"1"=>{:comment=>"test", :yen=>"1000", :position=>1, :index=>"1"}, "2"=>{:comment=>"", :yen=>"", :position=>2, :index=>"2"}} } assert_response :success assert_select 'tbody#journals_footer tr' do assert_select 'th', 3 assert_select 'th input[value=0]', 1 end end end
ãã¼ã...ç¸å¤ãããæ©ã¿ãªããã®ãã¹ãã ...ã
- æããã¦ãä¸è¨ã®ãã¹ãã³ã¼ãã§ã¡ããã¨ãã¹ãã§ãã¦ããã®ã ãããï¼
- rcovã¯ãã¹ãã³ã¼ãã«ãã£ã¦ä¸åº¦ã§ãå®è¡ãããé¨åã¯OKã¨ãã¦ãããããã ãããã¹ãã§ã©ããªãã§ãã¯ããã¦ããã¹ããã¯ä½ã£ã¦ãã人éã®ã¿ãç¥ã£ã¦ãã...ã
- Code coverageã100%ã ããã¨ãã£ã¦ãåç´ã«å®å¿ãã¦ãã¦ã¯ãããªããã¨ã«æ°ä»ããã
- ãã£ã¨ãã£ã¨ãã¹ãã®é人ãæ¸ããã³ã¼ããåå¼·ãã¦ããå¿ è¦ãããã
åèãã¼ã¸
以ä¸ã®ãã¼ã¸ãããã¸ãåèã«ãªãã¾ãããæè¬ã§ãï¼
- coverage
- ä¿è¨¼ç¯å²ã¨ããæ®åçãåæ±ãç¯å²ãªã©ã®æå³ãæã£ã¦ããããã ã