【问题标题】:Mocha JS: How to reference members from asynch before function in describe/it test functionsMocha JS:如何在描述/它测试函数中从异步函数之前引用成员
【发布时间】:2015-07-23 11:52:53
【问题描述】:

我正在尝试异步从配置文件在运行测试套件之前动态加载设置。 测试套件需要获取一个配置对象进行测试,并从中创建一个服务器连接 beforeEach 测试 - 然后关闭服务器连接 afterEach 测试。 我有以下代码大纲,但测试套件(嵌套 describe)总是在设置(before)函数完成之前调用;这意味着 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

【问题讨论】:

  • 作为快速修复,您可以使用fs.readdirSync()(我认为异步方式不可能,但我自己没有调查过)。
  • 呃!我希望有一些比不得不诉诸同步操作更优雅的东西。在这种情况下应该没问题,但是当没有同步函数可用时怎么办!?我的意思是不是 before[Each]/after[Each] 实用程序中的重点

标签: javascript asynchronous mocha.js bdd


【解决方案1】:

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

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 需要您的测试。

【讨论】:

  • 感谢您的回答。老实说,我希望有一些更有用的东西,因为这对于不存在同步替代方案的情况并没有真正的帮助。我希望这种测试设置有一个共同的范例......显然我可以先完成所有异步任务,然后将所有实际测试内容放入最终回调中,但这也很丑陋,我再次认为应该有更好的方法。
  • 更新了答案,例如在尚未加载配置时如何推迟 mocha 运行
  • 我正打算使用run() 编写一个解决方案 :-) 在此之前您必须使用--delay 运行Mocha(但您不必为此需要mocha) ,但除此之外,这是异步创建测试的好解决方案。
猜你喜欢
  • 2014-02-08
  • 2012-08-22
  • 1970-01-01
  • 1970-01-01
  • 2021-04-13
  • 2016-11-30
  • 2020-10-27
  • 2018-10-02
  • 1970-01-01
相关资源
最近更新 更多