学习nodeunit

我试图绕过node.js和nodeunit的asynchronous特性。

现在nodeunit应该帮助我,但它也让我头痛:

$ nodeunit test_logfile.js test_logfile.js ✖ testLogentry Error: Expected 1 assertions, 0 ran at Object.done (/home/mark/.node_libraries/.npm/nodeunit/0.5.1/package/lib/types.js:119:25) at /home/mark/devel/PerfDriver/test/test_logfile.js:48:10 at Object.runTest (/home/mark/.node_libraries/.npm/nodeunit/0.5.1/package/lib/core.js:54:9) at /home/mark/.node_libraries/.npm/nodeunit/0.5.1/package/lib/core.js:90:21 at /home/mark/.node_libraries/.npm/nodeunit/0.5.1/package/deps/async.js:508:13 at /home/mark/.node_libraries/.npm/nodeunit/0.5.1/package/deps/async.js:118:13 at /home/mark/.node_libraries/.npm/nodeunit/0.5.1/package/deps/async.js:134:9 at /home/mark/.node_libraries/.npm/nodeunit/0.5.1/package/deps/async.js:507:9 at Object.concatSeries (/home/mark/.node_libraries/.npm/nodeunit/0.5.1/package/deps/async.js:147:23) at Object.runSuite (/home/mark/.node_libraries/.npm/nodeunit/0.5.1/package/lib/core.js:79:11) FAILURES: 1/1 assertions failed (50ms) node.js:116 throw e; // process.nextTick error, or 'error' event on first tick ^ Error: ECONNREFUSED, Connection refused at Socket._onConnect (net.js:576:18) at IOWatcher.onWritable [as callback] (net.js:165:12) 

这里是我的testing用例:

 var nodeunit = require('../lib/nodeunit/lib/nodeunit.js') var http = require('http'); var logfile = require('../src/logfile.js'); var testmsg = 'this is my first testmessage:-_,.:;öüäß?1234"@€'; exports.testLogentry = function(test) { test.expect(1); // make sure all the async expectations are fulfilled var options = { host: 'localhost', port: 8123, path: '/', method: 'POST', record_message: function(msg) { console.log('received ' + msg + '\n'); test.equal(msg, testmsg); return; } }; var logger = logfile.loggerFactory(options); logfile.write(testmsg, options); logger.stop(); test.done(); }; 

我想我的问题是,如果断言在callback中工作正常,或者有任何限制可能导致testing用例失败。 我的假设是nodeunit能够应付asynchronous编程风格。 所以我期望“test.equal(msg,testmsg);” 上class。 如果我在节点控制台中运行testing用例的内容,则调用“record_message”。 我会发布logfile.js,但这可能是很多,我正在寻找更一般的build议。

谢谢。

标记

我不确定这是怎么处理的,但同时我find了解决问题的办法,我把它发布在这里,以防有人会为同样的问题而苦恼。

现在我可以说这个问题与node.js,http服务器和unit testing的asynchronous特性有关。

反正这里是我的解决scheme:

 var nodeunit = require('../lib/nodeunit/lib/nodeunit.js'); var http = require('http'); var logfile = require('../src/logfile'); var testmsg = 'this is my first testmessage:-_,.:;öüäß?1234"@€'; var testutil = function (config, envReady) { // simple testutil to provide http server environment for unit testing var logger = logfile.loggerFactory(config); // actually this is the tricky part which ensures that the server // is still available once request/ response are executed process.nextTick(function () { if (envReady && typeof envReady === 'function') { envReady(logger, logfile.write); } }); }; exports.test_logfile = function (test) { test.expect(4); var options = { host: 'localhost', port: 3000, path: '/simple', method: 'PUT', record_message: function(msg) { test.equal(msg, testmsg); } }; testutil(options, function (logger, write) { var write_ready = function(res) { // called when write is completed test.equal(res.statusCode, 201); test.equal(res.headers['content-type'], 'text/plain'); test.equal(res.body, 'created'); logger.stop(); test.done(); }; write(testmsg, options, write_ready); }); }; 

我认为在原始版本中有一些错误。 经过几个小时的睡眠,我将logger.stop()和test.end()调用移到了record_message函数的末尾。 现在断言默默“通过”,但错误是持久的。

 $ nodeunit test_logfile.js test_logfile.js node.js:116 throw e; // process.nextTick error, or 'error' event on first tick ^ Error: ECONNREFUSED, Connection refused at Socket._onConnect (net.js:576:18) at IOWatcher.onWritable [as callback] (net.js:165:12) 

以下是当前版本中的nodeunittesting用例:

 var nodeunit = require('../lib/nodeunit/lib/nodeunit.js') var http = require('http'); var logfile = require('../src/logfile.js'); var testmsg = 'this is my first testmessage:-_,.:;öüäß?1234"@€'; exports.testLogentry = function(test) { test.expect(1); // make sure all the async expectations are fulfilled var options = { host: 'localhost', port: 8123, path: '/', method: 'POST', record_message: function(msg) { console.log('received ' + msg + '\n'); test.equal(msg, testmsg); logger.stop(); setTimeout(test.done, 2000); } }; var logger = logfile.loggerFactory(options); logfile.write(testmsg, options); };