Bluebird Mocha中的每个循环都不起作用 [英] Bluebird Each loop in Mocha not working

查看:104
本文介绍了Bluebird Mocha中的每个循环都不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个测试,我需要循环遍历异步api调用的结果并动态生成mocha'It'来测试响应的每次迭代。我找到了一些让我开始的其他相关答案。这是我到目前为止所尝试的。

I am writing a test where I need to loop over the results of an async api call and dynamically make mocha 'Its' to test each iteration of the response. I found some other related answers which got me started. Here is what I have tried so far.

function getter(uri) {
    return new Promise(function(resolve, reject) {
        request({
            method: 'GET',
            json: true,
            uri: uri
        }, function(error, response, body) {
            if (response.statusCode == 200) {
                resolve(body);
            } else {
                reject(error);
            }
        });
    });
}

describe('This works', function() {
    it('works', function(done) {
        getter('myapi_that_returns_an_array').then(function(r) {
            r.should.not.be.empty;
            done();
        }).catch(function(err) {
            done(err);
        });
    });
});


describe('Why not this one', function() {
    getter('myapi_that_returns_an_array').each(function(r) {
        it('should make a test', function(done) {
            r.should.not.be.empty;
            done();
        });
    });
});

我试图将一个简单的数组包装在一个承诺中并将其提供给我的测试并且它有效!所以我无法弄清楚为什么我做的api调用不会以同样的方式工作。

I tried to just wrap a simple array in a promise and feed that to my test and it works! So I cannot figure out why the api call I make does not work the same way.

function simple_test() {
    return new Promise(function (resolve, reject) {
        resolve([ [1, 2, 3, 4], [2, 3, 4, 1], [3, 4, 2, 1], [4, 1, 2, 3] ]);
        });
}

describe('But this one works', function() {
    two().each(function(r) {
        it('is a test', function(done) {
            r.should.not.be.empty();
            done();
        });
    });
});

我试图给simple_test添加一个超时,使其像api调用一样 - 结果是与api电话相同。有没有办法用Mocha做到这一点?在等待承诺解析之前,似乎描述正在执行。

I tried to add a timeout to simple_test to make it act like the api call - the result is the same as the api call. Is there a way to do this with Mocha? It looks like the describe is executing before waiting for the promise to resolve.

推荐答案

尝试生成测试的测试版本通过调用里面的 getter.each 无法正常工作,因为Mocha没有规定异步生成测试这是你想要做的。正如我已经解释这里

The version of your test that tries to generate tests by calling it inside getter.each cannot work because Mocha has no provisions for generating tests asynchronously which is what you are trying to do. As I've explained here:


你得到的与Mocha如何发现你的测试有关。基本上Mocha是这样做的:

What you get has to do with how Mocha discovers your test. Basically Mocha does this:


  1. 读取所有测试文件并执行它们。传递给 describe 的回调会立即执行 。传递给 it 的回调以及挂钩(之前,之前等)都会记录以供日后使用执行

  1. Read all your test files and execute them. The callbacks passed to describe are executed right away. The callbacks passed to it and to the hooks (before, beforeEach, etc.) are recorded for later execution.

Mocha执行它记录的内容以供以后执行(根据一些合理的顺序,这里不重要)。

Mocha executes what it has recorded for later execution (according to some sensible order which is not important here).


尝试异步生成测试的问题是,当异步代码执行时,退出 describe 块,并忽略测试。在考虑 describe 阻止之前,没有办法告诉Mocha等待异步操作。

The problem with trying to generate tests asynchronously is that by the time the asynchronous code executes you're out of your describe block, and the tests are ignored. There is no way to tell Mocha to wait for an asynchronous operation before it considers a describe block over.

这篇关于Bluebird Mocha中的每个循环都不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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