Mocha测试用例-嵌套it()函数是否符合犹太标准? [英] Mocha test case - are nested it( ) functions kosher?

查看:167
本文介绍了Mocha测试用例-嵌套it()函数是否符合犹太标准?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种情况下,我想在Mocha测试中嵌套it()测试用例.我确定这是错误的,并且我看不到任何建议可以做我正在做的事情,但是目前我还真的不知道有什么更好的方法-

I have this case where I think I want to have nested it() test cases in a Mocha test. I am sure this is wrong, and I don't see any recommendations to do what I am doing, but I don't really know of a better way at the moment -

基本上,我有一个父"测试,在父测试中有一个forEach循环,其中包含所有子"测试:

basically, I have a "parent" test, and inside the parent test there's a forEach loop with all the "child" tests:

it('[test] enrichment', function (done) {

        var self = this;

        async.each(self.tests, function (json, cb) {

            //it('[test] ' + path.basename(json), function (done) {

                var jsonDataForEnrichment = require(json);
                jsonDataForEnrichment.customer.accountnum = "8497404620452729";
                jsonDataForEnrichment.customer.data.accountnum = "8497404620452729";
                var options = {
                    url: self.serverURL + ':' + self.serverPort + '/event',
                    json: true,
                    body: jsonDataForEnrichment,
                    method: 'POST'
                };


               request(options,function (err, response, body) {
                    if (err) {
                        return cb(err);
                    }

                     assert.equal(response.statusCode, 201, "Error: Response Code");
                     cb(null);


                });

            //});

        }, function complete(err) {
            done(err)
        });

    });

如您所见,注释了两行-我想将它们包括在内,以便我可以轻松地看到每个独立测试的结果,但是然后我遇到了尴尬的情况,即在回调的同时触发测试的回调用于async.each.

as you can see, two separate lines are commented out - I want to include them so that I can easily see the results of each separate test, but then I have this awkward situation of firing the callback for the test alongside the callback for async.each.

有没有人以前见过这种情况,并且知道一个好的解决方案,使测试人员可以轻松地循环查看每个测试的结果?

Has anyone seen this time of situation before and know of a good solution where the tester can easily see the results of each test in a loop?

推荐答案

不要嵌套it调用.同步调用它们.

Don't nest it calls. Call them synchronously.

嵌套it调用在Mocha中永远无法实现. it调用也不是异步执行的. ( test 可以是异步的,但是您不能异步调用 it.)这是一个简单的测试:

Nested it calls are never okay in Mocha. Nor are it calls performed asynchronously. (The test can be asynchronous, but you cannot call it asynchronously.) Here's a simple test:

describe("level 1", function () {
    describe("first level 2", function () {
        it("foo", function () {
            console.log("foo");
            it("bar", function () {
                console.log("bar");
            });
        });

        setTimeout(function () {
            it("created async", function () {
                console.log("the asyncly created one");
            });
        }, 500);
    });

    describe("second level 2", function () {
        // Give time to the setTimeout above to trigger.
        it("delayed", function (done) {
            setTimeout(done, 1000);
        });
    });
});

如果运行此命令,则不会得到嵌套测试bar,异步创建的测试(delayed)也将被忽略.

If you run this you won't get the nested test bar will be ignored and the test created asynchronously (delayed) will also be ignored.

Mocha对于​​此类调用没有定义的语义.当我在撰写本文时使用最新版本的Mocha(2.3.3)进行测试时,它只是忽略了它们.我记得较早版本的Mocha可以识别这些测试,但是会将它们附加到错误的describe块中.

Mocha has no defined semantics for these kinds of calls. When I ran my test with the latest version of Mocha at the time of writing (2.3.3), it just ignored them. I recall that an earlier version of Mocha would have recognized the tests but would have attached them to the wrong describe block.

这篇关于Mocha测试用例-嵌套it()函数是否符合犹太标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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