Misoca ã®ã³ã¼ãã¬ãã¥ã¼ã§æãã¦ããã£ã RSpec ãããã£ã¾ã¨ã
ãã®è¨äºã¯ Misoca+å¼¥ç Advent Calendar 2019 ã®7æ¥ç®ã®è¨äºã§ãã
ã¯ããã«
Misoca ã«å
¥ç¤¾ãã¦1å¹´ã¨ã¡ãã£ã¨ãçµã¡ã¾ããã
Misoca 㯠Ruby ãã¡ã¤ã³ã®ä¼ç¤¾ã§ãã
ç§ã®ãã£ãªã¢ã¯ãã£ã¨ Java ã ã£ãã®ã§ãç¹ã« RSpec ã®ç¥èã貧弱ã§ãRSpec ã¯ãexpect
ã£ã¦æ¸ãã®ã¯ç¥ã£ã¦ãâ¦ããããã®ç¥èã§ããã
RSpec ã®ãããã£ã¯æ°ãå¤ãããã£ããã©ãããåå¼·ãããããã®ãâ¦ã¨ããã¾ã¾ãã¹ãã³ã¼ããæ¸ãããããã³ã¼ãã¬ãã¥ã¼ã§ããããæ¸ãæ¹ãæãã¦ãããã¤ã¤ãã£ã¦ãã¾ããã
ããã§ãåãææãããããªãããã«èªåç¨ã«ã¾ã¨ãã¤ã¤ãåããããªå¢éã®äººã«å°ãã§ãå½¹ã«ç«ã¤ããã«ãMisoca ã®ã³ã¼ãã¬ãã¥ã¼ã§æãã¦ããã£ã RSpec ã®æ¸ãæ¹ãç´¹ä»ãã¾ãã
注æäºé ã¨ãã¦ã以ä¸ã§æ¸ãæãåã¨å¾ã®ã³ã¼ããåºã¦ãã¾ãããã©ã¡ããè¯ãæªãã¨ããæå³ã¯ããã¾ããã å®éãææãããã£ã¦ãæ¸ãæãããã¨ãããã°ãã¡ãªãããããã°ãã®ã¾ã¾ã«ãããã¨ãããã¾ããã
be_xxx
target
ã xxx? ã¨ããã¡ã½ãããæã£ã¦ããå ´åã be_xxx ã¨ãããããã£ã使ãã¾ãã
ä¾ãã°
expect(target.persisted?).to be_truthy
ã¨ãã example ããã£ãã
expect(target).to be_persisted
ã¨æ¸ããã¨ãã§ãã¾ãã
and
and
ãããã£ãã¤ãªãããã¨ã§ expect(target)
ãä½åº¦ãæ¸ããªãã¦ãã¿ã¾ãã
ä¾ãã°
expect(target).to be_cool expect(target).to be_beautiful
ã¨ãã example ããã£ãã
expect(target).to be_cool .and be_beautiful
ã¨æ¸ããã¨ã§ãã¾ãã
have_attributes
have_attributes
ãããã£ã使ããã¨ã§è¤æ°ã®å±æ§ãæã¤ãã¨ãä¸æ°ã«ãã§ãã¯ã§ãã¾ãã
ä¾ãã°
expect(target.name).to eq('mallowlabs') expect(target.company).to eq('Misoca Inc.') expect(target.joined_at).to eq('2018-10-01')
ã¨ãã example ããã£ãã
expect(target).to have_attributes( name: 'mallowlabs', company: 'Misoca Inc.', joined_at: '2018-10-01' )
ã¨æ¸ããã¨ãã§ãã¾ãã
all
all
ãããã£ã使ããã¨ã§ã³ã¬ã¯ã·ã§ã³ã®è¦ç´ ãä¸æ°ã«ãã§ãã¯ã§ãã¾ãã
targets.each do |target| expect(target).to be_ok end
ã¨ãã example ããã£ãã
expect(targets).to all(be_ok)
ã¨æ¸ããã¨ã§ãã¾ãã
satisfy
satisfy
ãããã£ã使ããã¨ã§ãããã¯ããããã£ã«å¤æã㦠all
ãããã£ã«æ¸¡ããã¨ãã§ãã¾ãã
ä¾ãã°
targets.each do |target| expect(target).not_to be_cool end
ã¨ãã example ããã£ãã
expect(targets).to all(satisfy { |t| !t.cool? })
ã¨æ¸ããã¨ãã§ãã¾ãã
contain_exactly
contain_exactly
ãããã£ã使ããã¨ã§ã³ã¬ã¯ã·ã§ã³ã®ä¸ã«è¦ç´ ããã¹ã¦å«ã¾ãã¦ãããã確èªã§ãã¾ãã
ã³ã¬ã¯ã·ã§ã³ã®é·ãã確èªãã¤ã¤ãè¦ç´ ãæå³ãããã®ãå
¥ã£ã¦ãããã®ãã§ãã¯ããéã«
array = ['mallowlabs'] expect(array.size).to eq(1) expect(array.first).to eq('mallowlabs')
ã¨ãã example ãæ¸ãã¦ãã¾ãããããã¯
array = ['mallowlabs'] expect(array).to contain_exactly('mallowlabs')
ã®ããã«æ¸ãã¾ãã
ä»ã«ã
ããããªãããã£ãæãã¦ãããã¾ããããã·ã³ãã«ã«èª¬æã§ããªãã£ãã®ã§å²æãã¾ãã
ã¾ã¨ã
æ´ç·´ããã¦ããªãã³ã¼ããæ¸ãã¦ã dis ããããã¨ãªãã建è¨çãªææ¡ããã¦ããã Misoca ã®éçºã¡ã³ãã¼ã®ã¿ããªã«æè¬ã§ãã
ææ¥ã¯ id:ryotaway ããBrainF***å¦çç³»ãä½ã£ã¦ã¿ã¾ããã¨ããè¨äºãè²·ãã¦ãããããã§ãã楽ãã¿ã§ããã