javascriptã§ãã¹ãé§åéçº Mocha+expect.js+testemãå¾ç·¨ã
ååã¯testemã使ã£ã¦èªåã§ãã¹ããèµ°ãç°å¢ä½ãããã¾ããã
ä»åãå¼ãç¶ãTDDã£ã½ããã¨ãã¦ããã¾ãã
addã¡ã½ããããã¹ããã
ååä½ã£ãcalcã¢ã¸ã¥ã¼ã«ã®addã¡ã½ããããã¹ããã¦ããã¾ãã
ã¾ãã¯æ°ããã¿ã¼ããã«ãç«ã¡ããã¦ã
ããã¸ã§ã¯ãã®ãã£ã¬ã¯ããªã«ç§»åãã¦testemãèµ·åãã¾ãã
$ cd ~/path/to/my/project $ testem
ããã§testemãç«ã¡ä¸ããã¾ããã
ã§ã¯æ¬¡ã«ãååä½ã£ããã¹ããã¡ã¤ã«ã«ãã¹ãã±ã¼ã¹ã追è¨ãã¦ã¿ã¾ãã
// test/calc.test.js var expect = require('expect.js'); var calc = require('../lib/calc.js'); describe('add', function(){ it('1ã¨1ãä¸ããã2ãè¿ã', function(){ var result = calc.add(1, 1); expect(result).to.be(2); }); // ãã¹ãã±ã¼ã¹ãè¿½è¨ it('1ã¨2ãä¸ããã4ã«ãªããªã', function(){ var result = calc.add(1, 2); expect(result).not.to.be(4); }); });
testemãå®è¡ãã¦ããã®ã§èªåã§ãã¹ããèµ°ãã
ã¿ã¼ããã«ã«ã¯
â 2 tests complete.
ã¨è¡¨ç¤ºããã¾ããã
ããã«ãã¹ãã±ã¼ã¹ã追è¨ãã¦ã¿ã¾ãã
// è¿½è¨ it('1ã¨"2"ãä¸ããã3ãè¿ã', function(){ var result = calc.add(1, "2"); expect(result).to.be(3); });
testemã«ã¯ä½ããã¨ã©ã¼ã®ãããªãã®ã表示ããã¾ããã
add 1ã¨"2"ãä¸ããã3ãè¿ã â Error: expected '12' to equal 3 at Assertion.assert (/path/to/myapp/node_modules/expect.js/expect.js:99:13) at Assertion.be.Assertion.equal (/path/to/myapp/node_modules/expect.js/expect.j s:200:10) at Assertion.(anonymous function) [as be] (/path/to/myapp/node_modules/expect.j s/expect.js:73:24) at Context.<anonymous> (/path/to/myapp/test/calc.test.js:15:22) at Test.Runnable.run (/path/to/username/.nave/installed/0.10.5/lib/node_modules/mocha/lib/runnabl e.js:213:32)
1è¡ç®ã«ãã¹ãã±ã¼ã¹ã®å
容ã2è¡ç®ã«å®éã®çµæã表示ããã¦ã¾ãã
ãã¹ãã±ã¼ã¹ã§ã¯çµæã¨ãã¦3ãè¿ã£ã¦ãããã¨ãæå¾
ãã¾ãããã
å®éã®ã¨ããã¯æååã®"12"ãè¿ã£ã¦ãã¾ããã
ããã¯JavaScriptã®ä»æ§ã§ã+æ¼ç®åã¯æååã¨ãã¦ã®çµåãåªå ããããã§ãã
ããã¯ãã°ã«ãªããã¨ãªã®ã§ãä»åä½ãaddã¡ã½ããã§ã¯
1+"2"ã3ã¨è¿ãããã«ä¿®æ£ãã¾ãã
// lib/calc.js // æ°å¤åã«å¤æããé¢æ°ä½ã£ã¦ã¿ã var castToNumber = function(v){ return typeof v === 'string' ? Number(v) : v; }; exports.add = function(a, b){ return castToNumber(a) + castToNumber(b); };
testemã®ç»é¢ã«ã¯ã
â 3 tests complete.
ã¯ãããã¹ããéãã¾ããï¼
ã»ã»ã§ãããã¾ã ç©´ãããã¾ãã
ããããããã¹ãã±ã¼ã¹ãä½æãã¾ãã
// ãã¹ãã±ã¼ã¹è¿½è¨ it('1ã¨{}ãä¸ããã1ãè¿ã', function(){ var result = calc.add(1, {}); expect(result).to.be(1); });
testemã¯ã
add 1ã¨{}ãä¸ããã1ãè¿ã â Error: expected '1[object Object]' to equal 1
ã¸ã¼ããããªçµæã«ãªãã®ãã
ã¨ãããã¨ã§ã³ã¼ããä¿®æ£ãã¾ãã
// æååã¯æ°å¤åã«å¤æãã¦ãä»ã¯0ãè¿ãããã«å¤æ´ãã¦ã¿ã var castToNumber = function(v){ if (typeof v === 'number') return v; if (typeof v === 'string') { return Number(v); } else { return 0; } }; exports.add = function(a, b){ return castToNumber(a) + castToNumber(b); };
testemã®çµæã¯ã
â 4 tests complete.
å
¨ã¦ã®ãã¹ãã±ã¼ã¹ããã¹ãã¦ã°ãªã¼ã³ã«ãªãã¾ããã
ãã¹ãé§åã£ã½ããã¨ããã¦ã¿ã¦
常ã«ãã¹ããèµ°ãç°å¢ã§éçºãããã¨ã§ã³ã¼ãã«èªä¿¡ãæã¦ããã
ãã°ãçãã¦ãããã«çºè¦ã§ããããã«ãªãã¾ãã
ãã¹ãã£ã¦ããã¨è»½ããã¥ã¢ã³ã¹ã«ãªã£ã¦ãã¾ãã¾ããã
Railsã¨ãã ã¨specï¼ä»æ§ï¼ã«ãã¹ãã³ã¼ããæ¸ãã®ã§ã
ãã¹ãã³ã¼ããæ¸ãã¨ãããã¨ã¤ã³ã¼ã«ä»æ§ãã³ã¼ãã§è¡¨ç¾ããã£ã¦ãã¨ã§ãã
ãã¹ãã³ã¼ããæ¸ãã¨ãã¯ã
- describe('ãã', function(){});
- ããã«ã¤ãã¦èª¬æãã¾ãã
- it('ãã', function(){});
- ããã¯ãããã§ãã
ã¨ãä»æ§ãæ確ã«ããªããæ¸ãã¦ããã¾ãã
ãã¨ãã°ãaddã¡ã½ããã¯å¼æ°ã2ã¤ã¨ãã¾ããã
å¼æ°ã1ã¤ãã渡ãããªãã£ãæã®ãµãã¾ãã¯ã©ããªãã§ãããï¼
ãã¹ãã±ã¼ã¹ã«ã¯ã¾ã ãã®è¨è¿°ãããã¾ããã
ã¤ã¾ãã試ãã¦ã¿ãªãã¨ããããªãç¶æ
ã§ãã
å¼æ°ã1ã¤ãã渡ãããªãã£ãã¨ãã
å®éã«ã¯bãundefinedã«ãªãã®ã§0ã«å¤æããã¾ããã
NaNãè¿ãããExceptionãè¿ãã¨ããé¸æè¢ãããã¾ãã
ä»ã®éçºè
ããã®çåãæã£ãã¨ããã¾ã£ããã«ãã¹ãã±ã¼ã¹ã«ç®ãéãã°
addã¡ã½ããã®ãµãã¾ããç解ã§ãããã¨ã§ãããã
ãããã«
ååãä»åã¨ãã¹ãåã§ããã
å®éã®æ¥åã§ã¯ã¬ãããªã¨ãã¹ãé§åéçºã§ãã¦ããªãã®ã§ã
ãããã£ã¦ã¿ãã¨ããã§ããã
ãã¯ãtestemãè¯ãæãã§ãã
次åã¯ããã³ãå´ãåå¼·ããããBackbone.jsãæ±ãäºå®ã§ãã