javascript - Mocha JS : How to reference members from asynch before function in describe/it test functions

标签 javascript asynchronous mocha.js bdd

我正在尝试运行测试套件之前异步从配置文件动态加载设置。 测试套件需要获取一个配置对象进行测试,并在 beforeEach 测试中从中创建服务器连接 - 然后关闭服务器连接 afterEach 测试。 我有以下代码大纲,但测试套件(嵌套描述)始终在之前设置(之前)函数完成时调用;这意味着 testConfigs 数组始终为空。 我想要做的事情是否可以实现,或者我是否必须从根本上改变测试?

describe('test server configs', function ()
{

    var testConfigs = [];

    before('get server configurations', function (done) {
        var conf = path.resolve('conf');
        fs.readdir(conf, function (err, files) {
                files.forEach(function (file) {
                    var config = require(path.join(conf, file));
                    testConfigs.push(config);
                });
            }

            console.dir(testConfigs); //prints the non-empty array
            done(err);
        });
    });

    describe('server test suite', function () {

        if (testConfigs.length == 0) {
            it('No server configurations to test!'); //0 tests passed, 1 pending
        }
        else {
            testConfigs.forEach(function (config) { //testConfigs is empty here
                var connection;

                beforeEach('connect to the server', function (done) {
                    connection = ServerConnection(config);
                    done();
                });

                it('should do something with the remote server', function (done) {
                    //test something with the 'connection' object
                    expect(connection.doSomething).withArgs('just a test', done).not.to.throwError();
                });

                afterEach('close connection', function (done) {
                    connection.close(done);
                });

            });
        }
    });

});

结果:

      test server configs
[ [ 'local-config.json',
    { host: 'localhost',
      user: 'username',
      pass: 'password',
      path: '/' } ],
  [ 'foo.com-config.json',
    { host: 'foo.com',
      user: 'foo',
      pass: 'bar',
      path: '/boo/far' } ] ]
    server test suite
      - No server configurations to test!


  0 passing (25ms)
  1 pending

最佳答案

当测试运行已经开始时,您可以添加新的测试。如果您想动态生成测试用例,您应该在测试开始之前进行。 您可以在测试之前使用同步方法读取配置

var testConfigs = []
var conf = path.resolve('conf');
var files = fs.readdirSync(conf);
files.forEach(function (file) {
    var config = require(path.join(conf, file));
    testConfigs.push(config);
});

describe('server test suite', function () {
   testConfigs.forEach(function (config) {
      //describe your dynamic test cases
   });
});

UPD 26.07.15

如果由于某些原因无法使用同步功能,您可以在安装完成后以编程方式运行mocha。

var Mocha = require('mocha');
var fs = require('fs');
var mocha = new Mocha({});

var testConfigs = global.testConfigs;
fs.readdir(conf, function (err, files) {
    files.forEach(function (file) {
        var config = require(path.join(conf, file));
        testConfigs.push(config);
    });
    mocha.run();
});

您必须通过全局范围传递testConfigs,因为mocha同步加载带有测试的模块,并且此配置必须准备好,直到mocha需要您的测试为止。

关于javascript - Mocha JS : How to reference members from asynch before function in describe/it test functions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31586583/

相关文章:

node.js - 如何监视 node.js 中定义的变量

node.js - 在 Mocha ExpressJS 测试期间获取 "Error: the string "不是有效的 BCrypt 哈希 ."was thrown, throw an Error :)"

javascript - 使用 webpack (Babel/ES6) 导入 moment-timzone 和 moment-range

php - jQuery:在隐藏的 Div 上实现 UI 自动完成

c# - 声明任务属性并等待它

ios - 异步加载大型 UIImages

javascript - Chai 期望 [Function] 抛出一个(错误)未通过测试(使用 Node)

javascript - 如何更改此联系表单的 Javascript,以便如果 key 是其他内容,则执行不同的操作?

javascript - NodeJS 将文本缓冲区转换为可查看的 HTML 页面

node.js - 仍然不理解 Node Js 异步函数