使用mocha测试异步函数 [英] Testing asynchronous function with mocha

查看:357
本文介绍了使用mocha测试异步函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试一个在node.js中运行的异步javascript函数,并向http api发出一个简单的请求:

I want to test a asynchronous javascript function that runs in node.js and makes a simple request to a http api:

const HOST = 'localhost';
const PORT = 80;

http = require('http');

var options = {
    host: HOST,
    port: PORT,
    path: '/api/getUser/?userCookieId=26cf7a34c0b91335fbb701f35d118c4c32566bce',
    method: 'GET'
};
doRequest(options, myCallback);

function doRequest(options, callback) {

    var protocol = options.port == 443 ? https : http;
    var req = protocol.request(options, function(res) {

        var output = '';
        res.setEncoding('utf8');

        res.on('data', function(chunk) {
            console.log(chunk);
            output += chunk;
        });

        res.on('error', function(err) {
            throw err;
        });

        res.on('end', function() {
            var dataRes = JSON.parse(output);
            if(res.statusCode != 200) {
                throw new Error('error: ' + res.statusCode);
            } else {
                try {
                    callback(dataRes);                        
                } catch(err) {
                    throw err;
                }
            }
        });

    });

    req.on('error', function(err) {
        throw err;
    });

    req.end();

}

function myCallback(dataRes) {
    console.log(dataRes);
}

执行此代码有效,响应将按预期显示。

Executed this code works and the response will be displayed as expected.

如果我在mocha测试中执行此操作,请求不会执行:

If I execute this in a mocha test the request is not executed:

describe('api', function() {
    it('should load a user', function() {
        assert.doesNotThrow(function() {
            doRequest(options, myCallback, function(err) {
                if (err) throw err;
                done();
            });
        });
        assert.equal(res, '{Object ... }');
    });
});

问题是,之后没有代码:

The Problem is, that no code after:

var req = protocol.request(options, function(res) {

甚至没有执行简单的console.log。

is executed not even a simple console.log.

任何人都可以帮忙吗?

推荐答案

你必须指定回调 done 作为提供给mocha的函数的参数 - 在这种情况下 it() function。如下所示:

You have to specify the callback done as the argument to the function which is provided to mocha - in this case the it() function. Like so:

describe('api', function() {
    it('should load a user', function(done) { // added "done" as parameter
        assert.doesNotThrow(function() {
            doRequest(options, function(res) {
                assert.equal(res, '{Object ... }'); // will not fail assert.doesNotThrow
                done(); // call "done()" the parameter
            }, function(err) {
                if (err) throw err; // will fail the assert.doesNotThrow
                done(); // call "done()" the parameter
            });
        });
    });
});

此外, doRequest(选项,回调)的签名指定两个参数,但是当您在提供三个参数的测试中调用它时。

Also, the signature of doRequest(options, callback) specifies two arguments though when you call it in the test you provide three.

Mocha可能找不到方法 doRequest(arg1,arg2,arg3)

Mocha probably couldn't find the method doRequest(arg1,arg2,arg3).

它没有提供一些错误输出吗?也许您可以更改mocha选项以获取更多信息。

Did it not provide some error output? Maybe you can change the mocha options to get more information.

编辑:

andho是对的,第二个断言将与 assert.doesNotThrow 并行调用,而它只应在成功回调中调用。

andho is right, the second assert would be called in parallel to assert.doesNotThrow while it should only be called in the success callback.

我修复了示例代码。

编辑2:

或者,为了简化错误处理(参见Dan M.的评论):

Or, to simplify the error handling (see Dan M.'s comment):

describe('api', function() {
    it('should load a user', function(done) { // added "done" as parameter
        assert.doesNotThrow(function() {
            doRequest(options, function(res) {
                assert.equal(res, '{Object ... }'); // will not fail assert.doesNotThrow
                done(); // call "done()" the parameter
            }, done);
        });
    });
});

这篇关于使用mocha测试异步函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆