Node.js ç¨ã®ãã¹ãã£ã³ã°ãã¬ã¼ã ã¯ã¼ã¯ Vows
æè¿ä½ãã¨è©±é¡ã® Node.jsï¼
Node.js æ¥æ¬ã¦ã¼ã¶ã°ã«ã¼ããããã¾ãï¼
ããã¥ã¡ã³ãã®ç¿»è¨³ãããã¦ã¾ã (ãªãªã¼ã¹ãããã°ããã® 0.2.2 対å¿æ¸ã¿ï¼)ï¼
ãã㪠Node.js åãã®ãã¹ãã£ã³ã°ãã¬ã¼ã ã¯ã¼ã¯ãããããããã¾ãï¼
ãã®ã¢ã¸ã¥ã¼ã«ä¸è¦§ã ãã§ã Node.js çéã®çãä¸ããã£ã·ããæãããã¾ããï¼ãããªä¸ãã Vows ã¨ãããã¹ãã£ã³ã°ãã¬ã¼ã ã¯ã¼ã¯ã§éãã§ã¿ãã®ã§ç´¹ä»ãã¾ãï¼
ã¡ãªã¿ã«ï¼ããã ãããããããä¸ãã Vows ãé¸ãã ã®ã¯ï¼
- ããã¥ã¡ã³ãããã£ãããã¦ãæ¹ï¼
- ãã¹ãã³ã¼ãã (ã¡ãã£ã¨ã ã) ã«ãã³ããï¼
ã¨ããã®ãçç±ã§ãï¼7 æå§ãã¾ã§ç±å¿ã«éçºãããå¾ï¼ã¡ãã£ã¨åæ»ãã¦ãããã«è¦ããªãããªããã©æ°ã«ããªãï¼
ã¡ãªã¿ã®ã¡ãªã¿ã«ï¼ä¸è¨ã¢ã¸ã¥ã¼ã«ãã¼ã¸ãç¡ä½çºã«ã¯ãªãã¯ãã¦ã©ããªãã¹ãã£ã³ã°ãã¬ã¼ã ã¯ã¼ã¯ã使ããã¦ããçºãã¦ã¿ãã®ã§ããï¼ãã¹ããå«ã¾ãªãã¢ã¸ã¥ã¼ã«ã¨ï¼èªåã§å°ããªãã¹ãç¨ã¢ã¸ã¥ã¼ã«ãç¨æãã¦ããã¢ã¸ã¥ã¼ã«ãåã
ã§ã»ã¨ãã©ãå ãã¦ãæãï¼ãã¨ã¯ V8 ã«ä»ãã¦ãã mjsunit.js ã使ã£ã¦ãã®ãå°ã
ï¼
ä»ããã¢ã¸ã¥ã¼ã«ã®å¤ãã¯ãã¹ãã£ã³ã°ãã¬ã¼ã ã¯ã¼ã¯ããªãææã«ä½ããããããã®ããããã¾ãããï¼ã¾ã ããã¨ãã£ã¦åºã¾ã£ã¦ãããã®ã¯ãªãããã§ãï¼
ã¤ã³ã¹ãã¼ã«
Node.js ã§ã¯ npm (Node Package Manager) ã¨ããããã±ã¼ã¸ããã¼ã¸ã£ãããã¡ã¯ãã«ãªã£ã¦ãã¦ããããã§ãï¼
Vows ã npm ããã¤ã³ã¹ãã¼ã«ããã®ãç°¡åã§ãï¼
npm install vows
ç°å¢ã«ãã£ã¦ã¯ sudo ãå¿
è¦ã§ï¼ã§ãããã 㨠npm ãããRunning npm as root is not recommended!ããªãã¦æãããããã¾ããæ°ã«ããªãï¼
æ¦è¦
ã¾ãã¯ç°¡åãªãµã³ãã«ããï¼
var vows = require('vows'), assert = require('assert'); vows.describe('MyFirstVows').addBatch( { 'ã¯ããã¦ã®Vows' : { topic : 'Hello', 'topicããã¹ãããã' : function(topic) { assert.equal(topic, 'Hello'); } } }).run();
æåã®è¡ã§ Vows ããã³æ¨æºã® 'assert'
ã¢ã¸ã¥ã¼ã«ãåãè¾¼ãã§ãã¾ãï¼
Node.js ã®ã¢ã¸ã¥ã¼ã«ã«é¢ãã¦ã¯ãã®è¾ºåç
§ï¼
- http://nodejs.jp/nodejs.org_ja/api.html#æ¨æºã¢ã¸ã¥ã¼ã«-1
- http://nodejs.jp/nodejs.org_ja/api.html#ã¢ã¸ã¥ã¼ã«-312
- http://wiki.commonjs.org/wiki/Modules
'assert'
ã¢ã¸ã¥ã¼ã«ã«é¢ãã¦ã¯ãã®è¾ºåç
§ï¼
ã§ï¼ãã®å¾ã® 1 æã Vows ã®ãã¹ãã³ã¼ãï¼
詳ããã¯å¾ã§è¦ããã¨ã«ãã¦ï¼ã¾ãã¯å®è¡ãã¦ã¿ã¾ãï¼
ä¸è¨ã®å
容ã my-first-vows.js
ã¨ãããã¡ã¤ã«ã«ä¿åãã¦ï¼
$ node my-first-vows.js · ✓ OK » 1 honored (0.002s)
ã£ã¦ãªæãã«ãªãã¾ãï¼
ã³ã¼ããã¡ãã£ã¨ä¿®æ£ãã¦
assert.equal(topic, 'Vows');
å®è¡ããã¨ï¼
$ node my-first-vows.js ✗ ã¯ããã¦ã®Vows ✗ topicããã¹ãããã » expected 'Vows', got 'Hello' (==) // my-first-vows.js:7 ✗ Broken » 1 broken (0.007s)
ã¨ãªãã¾ãï¼
ã¨ãããã¨ã§ï¼è©³ç´°ãè¦ã¦ããã¾ãï¼
ãã¹ãã¹ã¤ã¼ã
å
ã®ã³ã¼ãã®ãã¹ãæ¬ä½ãã°ãããªããè¦ã¦ããã¾ãï¼
ã¾ãã¯
var suite = vows.describe('MyFirstVows');
ã§ããã¹ãã¹ã¤ã¼ãããä½ã£ã¦ã¾ãï¼å¼æ°ã¯ãã®èª¬æã§ããï¼
ãã¹ãã¹ã¤ã¼ã㯠JUnit ãªããã¨åãã§ï¼è¤æ°ã®ãã¹ããã¾ã¨ãããã®ã®ããã§ãï¼
次ã«ï¼ãã®ãã¹ãã¹ã¤ã¼ãã«ãã¹ããå ãã¾ãï¼
suite.addBatch( { 'ã¯ããã¦ã®Vows' : { topic : 'Hello', 'topicããã¹ãããã' : function(topic) { assert.equal(topic, 'Hello'); } } });
ãã¹ãã¹ã¤ã¼ãã«è¿½å ããããã¹ãã®åä½ããããã¨å¼ã¶ã®ã§ããï¼
ãã®ãããã¯ãªãã¸ã§ã¯ãã§ï¼å¾è¿°ãããã³ã³ããã¹ããã¨å¼ã°ãããªãã¸ã§ã¯ããå¤ã¨ããããããã£ãæã£ã¦ãã¾ãï¼
ãã®ä¾ã¯ä¸ã¤ã®ã³ã³ããã¹ãããæã£ã¦ãã¾ãããï¼å½ç¶è¤æ°ã®ã³ã³ããã¹ãè¨è¿°ãããã¨ãã§ãã¾ãï¼
suite.addBatch( { 'ã³ã³ããã¹ã1' : {...}, 'ã³ã³ããã¹ã2' : {...}, 'ã³ã³ããã¹ã3' : {...}, ... });
ããã«å½ç¶ã®ãã¨ãªããï¼ãã¹ãã¹ã¤ã¼ãã«ã¯è¤æ°ã®ãããã追å ãããã¨ãã§ãã¾ãï¼
suite.addBatch( {...} ); suite.addBatch( {...} ); suite.addBatch( {...} ); ...
addBatch()
ã¯ãã¹ãã¹ã¤ã¼ãèªèº«ãè¿ãã®ã§ï¼ã¡ã½ãããã§ã¼ã³ã§
suite.addBatch( {...} ).addBatch( {...} ).addBatch( {...} )...;
ã¨æ¸ããã¨ãã§ãã¾ãï¼
éè¦ãªãã¤ã³ãã¨ãã¦ï¼ããããã®ãããã¯é次çã«å®è¡ããã¾ãï¼ãã®å¾åºã¦ãããããã®ä¸ã®ã³ã³ããã¹ãã¯éåæã«å®è¡ãããã®ã§ï¼ããã大ããªéãï¼
æå¾ã«ãã¹ãã¹ã¤ã¼ããå®è¡ãã¾ãï¼
suite.run();
ã¡ãªã¿ã«ï¼ãã¹ãã¹ã¤ã¼ããå®è¡ãã代ããã«ï¼ã¢ã¸ã¥ã¼ã«ã¨ãã¦å
¬éãããã¨ãã§ãã¾ãï¼
suite.export(module);
module
ã£ã¦ã®ã¯ Node.js ãç¨æãã¦ãããã°ãã¼ãã«ãªãã¸ã§ã¯ãã§ï¼ç¾å¨ã®ã¢ã¸ã¥ã¼ã«ããªãã¡ãã®ãã¡ã¤ã«èªèº«ã表ãã¾ãï¼
ãã¹ãã¹ã¤ã¼ããã¢ã¸ã¥ã¼ã«ã¨ãã¦å
¬éãããã¨ã¯ã©ããããã¨ãã¨ããã¨ï¼å¥ã®èª°ãããã®ãã¡ã¤ã«ãåãè¾¼ãã§å®è¡ã§ããã¨ãããã¨ã§ãï¼èª°ãï¼
ãããªãã㧠(ã©ããªããã§ï¼)ï¼Vows èªèº«ããã®èª°ããããç¨æãã¦ããã¦ã¾ãï¼
ããã使ãã¨ãã¹ãã¹ã¤ã¼ãã®å®è¡ã¯æ¬¡ã®ããã«ãããã¨ãã§ãã¾ãï¼
$ vows my-first-vows.js · ✓ OK » 1 honored (0.001s)
vows
ã£ã¦ã®ã¯ Vows ãç¨æãã¦ããã¦ãããã®ã§ããï¼ã·ã§ã«ã¹ã¯ãªããã¨ãã§ã¯ãªãï¼Node.js ã§å®è¡ããã JavaScript ãã¡ã¤ã«ã§ãï¼
ãã®å
é ã®è¡ã¯ãããªã£ã¦ã¾ãï¼
#!/usr/local/bin/node
ãããï¼Node.js ã§ã¯ #!
ã使ãããã ï¼ï¼ï¼
JavaScript ã®ææ³ã§ã¯ #
ã§å§ã¾ãè¡ãã³ã¡ã³ãã«ãªã£ããããããã§ã¯ãªãã®ã§ããï¼Node.js ã¯æåã®è¡ã #!
ã§å§ã¾ã£ã¦ããå ´åã«éãï¼ãããã³ã¡ã³ãã¨è¦ãªãã¦ãããããã§ãï¼
æåã®è¡ã #
ã§å§ã¾ã£ã¦ããã ãã§å¾ã« !
ããªãã£ããï¼2 è¡ç®ä»¥éã« #!
ããã£ã¦ãã³ã¡ã³ãã¨ã¯è¦ãªãããææ³ã¨ã©ã¼ã«ãªãã¾ãï¼
ã¨ããã (JW)ï¼vows
ã³ãã³ãã使ãã¨ã¯ã¤ã«ãã«ã¼ãã使ã£ã¦
$ vows test/*.js
ã¨ãã£ã¦ãã¹ãã¹ã¤ã¼ããã¾ã¨ãã¦å®è¡ã§ããããã«ãªãã¾ãï¼
ã³ã³ããã¹ã
ç¶ãã¦ãããã®ä¸ã«è¨è¿°ããã¦ããã³ã³ããã¹ãã§ãï¼
'ã¯ããã¦ã®Vows' : { topic : 'Hello', 'topicããã¹ãããã' : function(topic) { assert.equal(topic, 'Vows'); } }
ã³ã³ããã¹ãã¯ååã¨ããã説æ ('ã¯ããã¦ã®Vows'
) ãä»ãããããªãã¸ã§ã¯ãã§ãï¼
ã³ã³ããã¹ãã¯ãã®å
容ã¨ãã¦ï¼ä¸ã¤ã®ããããã¯ãã¨ï¼ãã®ãããã¯ã«å¯¾ãããã¹ããè¨è¿°ããè¤æ°ã®ãvowããæã¤ãã¨ãã§ãã¾ãï¼
ãããã¯ã¨ããã®ã¯ topic
ã¨ããååã®ããããã£ã§ï¼ã³ã³ããã¹ãã«ããããã¹ãã®å¯¾è±¡ã§ãï¼RSpec ã® subject
ã¿ãããªãã®ã§ããããï¼ä¸è¨ã®ä¾ã§ã¯ 'Hello'
ã¨ããæååããããããã¹ããããã¨ãã¦ãããã¨ã«ãªãã¾ãï¼
vow ã¨ããåèªã¯ãæè¨ããããç´æããããå
¬ç´ããããªã©ã®æå³ãããããã§ãï¼ç¥ããªãã£ããï¼ãããã¯ãã©ããªã£ã¦ããªããã°ãããªãã®ããæè¨ããããã§ããï¼
vow ã¯ååã topic
以å¤ã®ããããã£ã§ï¼å¼æ°ã«ãããã¯ãåãåãé¢æ°ã¨ãã¦è¨è¿°ãã¾ãï¼
ã³ã³ããã¹ãã¯ãã¹ããããã¨ãã§ãã¾ãï¼
'ã¯ããã¦ã®Vows' : { topic : 'Hello', 'topicããã¹ãããã' : function(topic) { assert.equal(topic, 'Hello'); }, 'ãã¹ãããã³ã³ããã¹ã' : { topic : 'Vows', 'ãã¹ãããã³ã³ããã¹ãã®topicããã¹ãããã' : function(topic) { assert.equal(topic, 'Vows'); } } }
ãããã¯ï¼
'ã¯ããã¦ã®Vows' : { 'ãã¹ãããã³ã³ããã¹ããã®1' : { topic : 'Hello', 'ãã¹ãããã³ã³ããã¹ããã®1ã®topicããã¹ãããã' : function(topic) { assert.equal(topic, 'Hello'); } }, 'ãã¹ãããã³ã³ããã¹ããã®2' : { topic : 'Vows', 'ãã¹ãããã³ã³ããã¹ããã®2ã®topicããã¹ãããã' : function(topic) { assert.equal(topic, 'Vows'); } } }
ã¨ãããã¨ãã§ãã¾ãï¼
ããã¾ã§ãã¾ã¨ããã¨ï¼Vows ã«ããããã¹ãã¹ã¤ã¼ãã®æ§é ã¯ï¼
- ãã¹ãã¹ã¤ã¼ãã¯è¤æ°ã®ããããæã¤ï¼
- ãããã¯è¤æ°ã®ã³ã³ããã¹ããæã¤ï¼
- ã³ã³ããã¹ãã¯
- 0 ã¾ã㯠1 åã®ãããã¯ãæã¤ï¼
- è¤æ°ã® vow ãæã¤ï¼
- è¤æ°ã®ãµãã³ã³ããã¹ããæã¤ï¼
ã¨ãããã¨ã«ãªãã¾ãï¼
ããã¦ï¼åã³ã³ããã¹ãã¯éåæã«å®è¡ããã¾ãï¼
ããã¯ï¼ã³ã³ããã¹ããè¨è¿°ããé çªã«å®è¡ãããã¨ã¯éããªããã¨ãæå³ãã¾ãï¼ã¾ãï¼åã³ã³ããã¹ããä¸ã¤ãã¤å®è¡ãããããã§ã¯ãªããã¨ãæå³ãã¾ãï¼éåæãªã¤ãã³ãé§åã® Node.js ããããã¹ãã£ã³ã°ãã¬ã¼ã ã¯ã¼ã¯ã§ããï¼
ãããã¯
ããã¾ã§ã®ä¾ã§ã¯ãããã¯ã«æååãªãã©ã«ããæå®ãã¦ãã¾ããã§ãããï¼é¢æ°ã使ããã¨ãã§ãã¾ãï¼
topic : function() { return 'Hello'; },
é¢æ°ãªã®ã§ï¼ãããããªãã¨ãã§ãã¾ãï¼æ®éã¯ãã®ä¸ã§ãã¹ã対象ã®ãªãã¸ã§ã¯ããçæãã¦è¿ãããï¼é¢æ°ãå¼ã³åºãã¦ãã®çµæãè¿ããããããã¨ã«ãªãã§ãããï¼
ãããã¯ãé¢æ°ã®å ´åï¼ãã®é¢æ°ãè©ä¾¡ããçµæã vow ã«æ¸¡ããã¾ã (ã¡ãã£ã¨åï¼è©³ç´°ã¯å¾ã»ã©)ï¼
ã³ã³ããã¹ãã«è¤æ°ã® vow ããã£ã¦ãï¼ãããã¯ãè©ä¾¡ãããã®ã¯ä¸åº¦ã ãã§ãï¼ä¾ãã°
var i = 0; ... topic : function() { return i++; }, 'topicããã¹ãããã0' : function(topic) { assert.equal(topic, 0); }, 'topicããã¹ãããã1' : function(topic) { assert.equal(topic, 1); }
ã¨ããå ·åã«ï¼vow ãã¨ã«ãããã¯ãè©ä¾¡ããããã¨ãæå¾ ãããã¹ãã¯å¤±æãã¾ãï¼
✗ ã¯ããã¦ã®Vows ✗ topicããã¹ãããã » expected 1, got 0 (==) // my-first-vows.js:14 ✗ Broken » 1 honored ∙ 1 broken (0.005s)
両æ¹ã® vow ã«åãå¤ 0
ã渡ããããã¨ãåããã¾ãï¼
ããã§ã³ã³ããã¹ããåãã¦ã¿ã¾ãï¼
'ã³ã³ããã¹ã1' : { topic : function() { return i++; }, 'topicããã¹ãããã' : function(topic) { assert.equal(topic, 0); } }, 'ã³ã³ããã¹ã2' : { topic : function() { return i++; }, 'topicããã¹ãããã' : function(topic) { assert.equal(topic, 1); } }
ãã®å ´åï¼ç°ãªãã³ã³ããã¹ãã®ãããã¯ã¯ç¬ç«ã«å®è¡ããã¾ããï¼è¨è¿°ããé ã§å®è¡ãããã¨ã¯éãã¾ããï¼
èªåã®ç°å¢ã§ã¯ãã®ãã¹ãã¯å¸¸ã«æåãã¦ãã¾ãã¾ããï¼ãã£ã¨è¤éãªãã¹ãã§ã¯å¤±æãããã¨ããããããããªãã®ã§æ³¨æãå¿
è¦ã§ãï¼
ã¾ãï¼ä»ã®ãã¹ãã«ä¾åãããããªãã¹ããæ¸ããªããã°ããã ãã§ããï¼
ãã¹ãããã³ã³ããã¹ãã¨ãããã¯
åè¿°ã®ããã«ï¼ã³ã³ããã¹ãã¯ãã¹ããããã¨ãã§ãã¾ãï¼ããã¦ããããã®ã³ã³ããã¹ãã¯ãããã¯ãæã¤ãã¨ãã§ãã¾ãï¼
ãããã¯ãé¢æ°ã¨ããå ´åï¼è¦ªã®ã³ã³ããã¹ãã®ãããã¯ãå¼æ°ã¨ãã¦åãåããã¨ãã§ãã¾ãï¼
'親ã³ã³ããã¹ã' : { topic : "Foo", 'åã³ã³ããã¹ã' : { topic : function(parent) { return parent + 'Bar'; }, 'topicããã¹ãããã' : function(topic) { assert.equal(topic, 'FooBar'); } } }
親ã ãã§ãªãï¼ç¥å ã®ãããã¯ãåãåããã¨ãã§ãã¾ãï¼
'ãããã¡ããã³ã³ããã¹ã' : { topic : 'Foo', '親ã³ã³ããã¹ã' : { topic : "Bar", 'åã³ã³ããã¹ã' : { topic : function(parent, grandpa) { return grandpa + parent + 'Baz'; }, 'topicããã¹ãããã' : function(topic) { assert.equal(topic, 'FooBarBaz'); } } } }
ã²ããããã¡ããã³ã³ããã¹ãããããªã
topic : function(parent, grandpa, grandgrandpa) {
ã¨ããããã«ï¼ããã¤ã§ãä¸ä½ã®ãããã¯ãåãåããã¨ãã§ãã¾ãï¼
ã¡ãªã¿ã«ä¸ä½ã¨ãç¥å
ã¨ãã£ã¦ããã®ã¯ããã¾ã§ããããã¯ã®ãã¨ã§ãã£ã¦ã³ã³ããã¹ãã®ãã¨ã§ã¯ããã¾ããï¼
ä¾ãã°
'ãããã¡ããã³ã³ããã¹ã' : { topic : 'Foo', '親ã³ã³ããã¹ã' : { 'åã³ã³ããã¹ã' : { topic : function(parent) { return parent + 'Bar'; }, 'topicããã¹ãããã' : function(topic) { assert.equal(topic, 'FooBar'); } } } }
ãã®ããã«éä¸ã«ãããã¯ã®ãªãã³ã³ããã¹ã (親ã³ã³ããã¹ã) ãæã¾ã£ã¦ãã¦ãé¢ä¿ãªãï¼å¼æ°ã®æ°ã ããããã¯ãæã£ãã³ã³ããã¹ãã辿ã£ã¦ãã£ã¦ããã¾ãï¼
éåæã¤ãã³ãã®ãã¹ã
Node.js ã¯éåæãªã¤ãã³ãé§åã®ãã©ãããã©ã¼ã ãªã®ã§ï¼ãã®ã¢ããªã±ã¼ã·ã§ã³ã¯ã¤ãã³ããã³ãã©ãå¤ç¨ãããã¨ã«ãªããã¡ã§ãï¼
ãã㧠Vows ã¯éåæã¤ãã³ãã®ãã¹ããããããã« 2 ã¤ã®æ¹æ³ãæä¾ããã¦ãã¾ãï¼
this.callback
ã使ãï¼EventEmitter
ã«ããããããã¹ãã使ãï¼
ãããããããã¯ã®ç¹æ®ãªä½¿ãæ¹ã«ãªãã¾ãï¼
this.callback
ãããã¯é¢æ°ã® this.callback
ã«ã¯ï¼ãã®ãããã¯ã¨åãã³ã³ããã¹ãã® vow ãå¼ã³åºãã¦ãããé¢æ°ãè¨å®ããã¦ãã¾ãï¼
ããããããã¯ã®ä¸ã§ã³ã¼ã«ããã¯é¢æ°ã¨ãã¦ä¸ããã¨ï¼vow ãããããã³ã¼ã«ããã¯é¢æ°ã§ãããã®ããã«ãªãã¾ãï¼
ä¾ãã°
topic : function() { setTimeout(this.callback, 1000, null, 'timedout'); }, '1ç§å¾ã«topicããã¹ãããã' : function(err, topic) { assert.equal(topic, 'timedout'); }
setTimeout()
㯠Node.js ãç¨æãã¦ããã¦ããã°ãã¼ãã«é¢æ°ã§ãï¼
ããã使ã£ã¦ï¼1 ç§å¾ã«ã¿ã¤ã ã¢ã¦ããã㨠this.callback
ãäºã¤ã®å¼æ°ãä¼´ã£ã¦å¼ã³åºãããããã«ãã¦ãã¾ãï¼ãªã®ã§ï¼1 ç§çµéããã¨
this.callback(null, 'timedout');
ã¨ããå¼ã³åºãã (Node.js ã«ãã£ã¦) è¡ããã¾ãï¼ãã㦠Vows ã¯ãã®å¼æ°ãã®ã¾ã¾ã§ vow ãå¼ã³åºãã¦ããã¾ãï¼
this.callback
ã¯ä¸»ã«ãã¡ã¤ã«å
¥åºåãªã©ã§ä½¿ããã
function(err, arg1, arg2...)
ãªå½¢å¼ã®ã³ã¼ã«ããã¯ãæ³å®ãã¦ããããã§ãï¼
ã¤ã¾ããã®ä¾ã§ã¯ï¼this.callback
ã«æ¸¡ãããæåã®å¼æ°ã¯ã¨ã©ã¼ããªããã¨ã示ã null
ï¼æ¬¡ã®å¼æ°ãæ¬æ¥ã®ãããã¯ã§ããæååã¨ããããã§ãï¼ã¡ãªã¿ã«æåã®å¼æ°ã null
ã undefined
ã§ãªãå ´åï¼this.callback
ã¯ãã¹ãã失æããã¨å¤æãã¾ãï¼ã¨ã©ã¼ãèµ·ããå ´åã®ãã¹ã㯠this.callback
ã§ã¯ã§ããªãï¼ï¼ï¼ ã¨ãããã¨ã ãããï¼
ã¨ããã (JW)ï¼vow ã¯ãã® this.callback
ã®å¼æ°ããã®ã¾ã¾åãåããã¨ãã§ãã¾ãï¼ãã ãï¼æåã®å¼æ° err
ãä¸è¦ã§ããã° (æ®éã¯ä¸è¦ã§ã)ï¼
'1ç§å¾ã«topicããã¹ãããã' : function(topic) {
ã¨ãããã¨ãã§ãã¾ãï¼
ãã®ããã«ï¼this.callback
ã使ãã¨ããããªé¢¨ã«ã³ã¼ã«ããã¯ããããã¨ããªãã¹ããç°¡åã«è¨è¿°ãããã¨ãã§ãã¾ãï¼ãã¹ã対象ãã¤ãã³ãã½ã¼ã¹ã®å ´åã«æå¹ã§ããï¼
ãããã¯ã®é¢æ°ãå¤ãè¿ãã¦ããªããã¨ã«ã注ç®ï¼å¤ãè¿ãã¨ããã vow ã«æ¸¡ããã¦ãã¾ãã¾ãï¼
ãããã¹
this.callback
ããããã«å¼·åã§æè»ãªã®ããããã¹ã§ãï¼
ãããã¹ã¨ããã®ã¯éåæã«å®è¡ããã¦ãã¤ãçµæãå¾ãããã¨ãããã®ã§ï¼Java ã§ãã㨠FutureTask
ã¿ãããªï¼
Vows åºæã®ç¨èªã§ã¯ãªãéåæçéã§ã¯ãã使ããã¦ãã¦ï¼CommonJS ã§ãææ¡ããã¦ã¾ãï¼
ãªãã¸ã§ã¯ãæåã§ã 80 年代ã®éæ¨æ·³ããã® Smalltalk æ¬ãªããã«åºã¦ãã¦ãæ°ãããã®ã§ï¼ããªãå¤ãããããæ¦å¿µã ã¨æããã¾ãï¼
æè¿åºãã°ããã®æ¸ç±ãTest-Driven JavaScript Developmentãã«ã¯ãChapter 14. Server-Side JavaScript with Node.jsãã¨ããç« ããã£ãããã£ã¦ï¼ããã«ãSection 14.4. Promisesãã¨ããç¯ãããã¾ãï¼ããªãæ®éã«ä½¿ããã¦ãããã§ããï¼
Test-Driven JavaScript Development (Developer's Library)
- ä½è : Christian Johansen
- åºç社/ã¡ã¼ã«ã¼: Addison-Wesley Professional
- çºå£²æ¥: 2010/09/09
- ã¡ãã£ã¢: ãã¼ãã¼ããã¯
- ã¯ãªãã¯: 112å
- ãã®ååãå«ãããã° (7件) ãè¦ã
70 年代ãããã£ãããã§ãï¼ã¾ããã¥ã¼ã¤ãããããï¼ï¼ï¼
</追è¨>
Node.js ã«ããã¦ã¤ãã³ããçºçãããã®ã¯ EventEmitter
ã§ãï¼
ãããã¯ã§ãããã¹ã使ãã¨ããã®ã¯ï¼ãã® EventEmitter
ãæ»ãå¤ã¨ãã¦è¿ãã¨ãããã¨ã§ãï¼ãã㦠EventEmitter
ã 'success'
ã¤ãã³ããçæããã¨ï¼ããã«ãã£ã¦ vow ãå¼ã³åºããã¾ã (ã¨ã©ã¼ã¤ãã³ãã®ãã¹ãã¯ï¼ï¼ï¼ï¼)ï¼
ä¾ãã°
topic : function() { var promise = new (events.EventEmitter); setTimeout(function() { promise.emit('success', 'timedout'); }, 1000, promise); return promise; }, '1ç§å¾ã«topicããã¹ãããã' : function(topic) { assert.equal(topic, 'timedout'); }
ã¡ãªã¿ã« vow ã®å¼æ°ã§ã¯çºçããã¤ãã³ãã®å¼æ°ãå ¨ã¦åãåããã¨ãã§ãã¾ãï¼
promise.emit('success', arg1, arg2, arg3, arg4);
ã¨ããã¨ï¼
'1ç§å¾ã«topicããã¹ãããã' : function(type, arg1, arg2, arg3, arg4) {
ã®ããã«ãããã¨ãã§ãã¾ãï¼å
é ã® type
ã¯ã¤ãã³ãã®ç¨®é¡ã§ï¼ããã§ã¯ 'success'
ã§ãï¼æ®éã¯ãããªã ('success'
ã®å ´åããå¼ã°ããªããã) ã®ã§çç¥ãã¾ãï¼
å®ã¯ãããã¯ã®æ»ãå¤ã EventEmitter
以å¤ã®å ´åï¼Vows ã®å
é¨ã§ EventEmitter
ãä½ããã¦ãã¾ãï¼ãããã¹ä»¥å¤ã®æ¹æ³ã¯ï¼ãããã¹ãé è½ãã¦ããã¦ããé«æ°´æºãªããæ¹ã¨ããã ããªã®ã§ããï¼
ãããã¯ã®ã¾ã¨ã
å¤å°ä¸æ£ç¢ºããããã¾ãããï¼ãããã¯ãæ´çããã¨æ¬¡ã®ããã«ãªãã¾ãï¼
- ãããã¯ãé¢æ°ã®å ´åã¯ãã®é¢æ°ãè©ä¾¡ãï¼
- æ»ãå¤ã
undefined
ã®å ´åã¯ï¼this.callback
ã®ç¬¬1å¼æ°ãnull
ã¾ãã¯undefined
ã§å¼ã³åºãããã¨ï¼ãã®å¼æ°ããããã¯ã¨ã㦠vow ãå¼ã³åºãããï¼ - æ»ãå¤ã
undefined
以å¤ã®å ´åã¯ãã®å¤ããããã¯ã¨ãã¦ä»¥ä¸ãç¶ç¶ï¼
- æ»ãå¤ã
- ãããã¯ããããã¹ (
EventEmitter
) ã®å ´åï¼- ãããã¹ã
'success'
ã¤ãã³ããçæããã¨ï¼ã¤ãã³ããªã¹ãã¼ã«æ¸¡ãããå¼æ°ããããã¯ã¨ã㦠vow ãå¼ã³åºãããï¼
- ãããã¹ã
- ãã以å¤ã®å ´å
- ãããã¯ã®å¤ãå¼æ°ã¨ã㦠vow ãå¼ã³åºãããï¼
表æ
Vows ã¯æ¨æºã® assert
ã¢ã¸ã¥ã¼ã«ãæ¡å¼µãã¦ï¼ããã¤ãã®é¢æ°ã追å ãã¦ããã¾ãï¼è©³ç´°ã¯ãã¡ããã©ãã¼ï¼
Vows ã§ã Node.js ã§ããªã CommonJS ã® Unit Testing 1.0 ã®è©±ã«ãªãã¾ããï¼
assert.equal(actual, expected)
㯠JUnit ãªããã¨éã§ï¼ç¬¬ 1 å¼æ°ãå®éã®å¤ï¼ç¬¬ 2 å¼æ°ãæå¾
å¤ã§ãï¼JUnit ã«æ
£ãã人ã¯æ³¨æãå¿
è¦ããï¼å人çã«ã¯ãââã¯ÃÃã§ãããã¨ãã®æ¹ãèªç¶ãªã®ã§ãããããããªï¼
ããããï¼ä¸ã®æ¹ã«æ¸ããã¢ã¸ã¥ã¼ã«ä¸è¦§ã«ã¯è¡¨æã ãã®ã¢ã¸ã¥ã¼ã«ã¨ãï¼ã¢ã㯠(ï¼ ãã£ã¡ã§ã¯ spy ã¨ãå¼ã¶ããã) é¢é£ã®ã¢ã¸ã¥ã¼ã«ãªãããããã®ã§è¦ãã§ãã¯ã§ãï¼
ã¾ã¨ã
ã¨ãããã¨ã§ï¼ãã£ãã㨠Vows ã®æ©è½ã«ã¤ãã¦ç´¹ä»ãã¾ããï¼ã¨ã¯ããï¼æçã°ããã§ãã¹ãã³ã¼ãã®å
¨ä½åã¨ãæ¬ç©ã®éåæã¤ãã³ãã使ã£ããã¹ããã©ããªãã®ãä¼ãããªãã§ãããï¼
ææ¥ã¯ããã¡ãã£ã¨ã¾ã¨ã㪠(ã¨ã¯ãã£ã¦ãç°¡åãª) ä¾ã使ã£ã¦ Vows ã®ãã¹ãã³ã¼ããæ¸ãã¦ã¿ããã¨æãã¾ãï¼