æè¿ã® RSpec ã¯ãããã¾ã§
obj.stub(hoge: value)
ã¨æ¸ãããã®ãã
allow(obj).to receive(:hoge).and_return value
ã¨æ¸ããªãã¨ãããªããªã£ããã¨ããæ£æ°ã®æ²æ±°ã¨ã¯æããªããããªå¤æ´ããããããã®ã§ãä½å¹´ãã¶ãã« Test::Unit ã使ã£ã¦ã¿ããã¨ãªãããªä¸ã§ãã
RSpec ã¯ããã¹ãã±ã¼ã¹ãå ¥ãåã«ã§ãããããã¹ãã±ã¼ã¹ã example ãã¯ã©ã¹ãã¡ã½ããã§ã¯ãªããæååã§èªç±ã«æ¸ããã¨ãã§ãããããã®ãè¯ãã£ãã®ã§ãããæè¿ã® Test::Unit ã§ã¯ãããã§ããããã«ãªã£ã¦ãã¾ãã
[ruby-list:48926] [ANN] test-unit 2.5.2
ãã®ãªãªã¼ã¹ã¯ã¨ã¿ãããã«ä½¿ã£ã¦ããããããã«æ¹è¯ãããªãªã¼ ã¹ã§ããæ°ãã追å ãã--locationã¯RSpecã®--line_numberã¨ä¼¼ã æ©è½ã§ããsub_test_caseã¯shoulda-contextã¨RSpecã®contextã¨ä¼¼ ãæ©è½ã§ãããããã®æ©è½ãããã°test-unit 2ã«ä¹ãæãã¦ãã ãã¨èããã®ã§å®è£ ãã¾ããã
2å¹´ããã£ã¦ãã£ã¨ä½¿ãå§ãã¾ãããããã¾ããâ¦ã
æºå
% gem install test-unit
ãã¹ã
RSpec ã§æ¬¡ã®ããã«æ¸ãã¦ããã¹ãã¯ã
describe Array do context '空ã®å ´å' do before do @array = [] end it 'size ã 0' do expect(@array.size).to eq 0 end end context 'è¦ç´ ã1ã¤ã®å ´å' do before do @array = ['hoge'] end it 'size ã 1' do expect(@array.size).to eq 1 end end end
Test::Unit ã ã¨æ¬¡ã®ããã«æ¸ãã¾ãã
require 'test/unit' class TestArray < Test::Unit::TestCase sub_test_case '空ã®å ´å' do setup do @array = [] end test 'size ã 0' do assert_equal 0, @array.size end end sub_test_case 'è¦ç´ ã1ã¤ã®å ´å' do setup do @array = ['hoge'] end test 'size ã 1' do assert_equal 1, @array.size end end end
ä¸çªå¤å´ã¯ Test::Unit::TestCase
ãç¶æ¿ãã¦ã¯ã©ã¹ãä½æããå¿
è¦ãããã¾ããããã®ã¯ã©ã¹ã®ä¸ã¯ sub_test_case
ã使ã£ã¦ä»»æã®æååã§ãã¹ãã±ã¼ã¹ãè¨è¿°ã§ãã¾ãã
RSpec ã® describe
, context
ã¨åæ§ã« sub_test_case
ã¯å
¥ãåã«ãã§ãã¾ãã
ãã ããtest
ã«æå®ããæååãã¾ã£ããåãã ã¨å¾ã§å®ç¾©ããæ¹ã¯ç¡è¦ããã¦ãã¾ããããªã®ã§æ³¨æãã¾ãããã
ãã¹ã¯ãããéç¥
gem 㧠test-unit-notify ãã¤ã³ã¹ãã¼ã«ãã¦ãã¹ã¯ãªããã®å é ã§
require 'test/unit/notify'
ã¨æ¸ãã¦ããã¨ããã¹ã¯ãããéç¥ã§çµæãç¥ããã¦ããã¦ä¾¿å©ã§ãã
stub
stub ã¯ãtest-unit-rr ãã¤ã³ã¹ãã¼ã«ãã¦ãã¹ã¯ãªããã®å é ã§
require 'test/unit/rr'
ã¨ãã¦ããã次ã®ããã«è¨è¿°ãããã¨ãã§ãã¾ãã
stub(obj).hoge{value} # obj ã® hoge ã¡ã½ããã value ãè¿ã
RSpec ãããã·ã³ãã«ã«æ¸ãã¦è¯ãã§ãã