【问题标题】:learning nodeunit学习节点单元
【发布时间】:2011-07-10 15:24:19
【问题描述】:

我试图绕开 node.js 和 nodeunit 的异步特性。

现在 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)

这是我的测试用例:

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();
};

我想我的问题是断言是否在回调中运行良好,或者是否有任何限制可能导致测试用例失败。 我的假设是 nodeunit 能够应对异步编程风格。所以我期待“test.equal(msg, testmsg);”去工作。如果我在节点控制台中运行测试用例的内容,则会调用“record_message”。 我也会发布 logfile.js,但这可能太多了,我正在寻找更一般的建议。

谢谢。

标记

【问题讨论】:

  • 那你为什么在我回答完你之前的问题后就直接删除了?我的建议是否正确?你不喜欢我最后批评了“风格”(出于某种原因 - 而且不是不合理的!) - 但从不费心回应这个建议。不完全是一个好的举动。

标签: unit-testing node.js


【解决方案1】:

我认为在原始版本中存在一些错误。经过几个小时的睡眠后,我将 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)

这是当前版本的 nodeunit 测试用例:

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);
};

【讨论】:

    【解决方案2】:

    我不确定这里一般是如何处理的,但同时我已经找到了我的问题的解决方案,我将其发布在这里,以防有人遇到同样的问题。

    现在我可以看出问题与 node.js 的异步特性、http 服务器和单元测试在这些情况下有关。

    无论如何,这是我的解决方案:

    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);
        });
    };
    

    【讨论】:

      猜你喜欢
      • 2012-04-21
      • 1970-01-01
      • 1970-01-01
      • 2015-11-01
      • 2011-01-15
      • 2014-08-17
      • 1970-01-01
      • 2023-03-21
      • 2011-11-06
      相关资源
      最近更新 更多