itParam不采用全局数据数组参数的修改后的值

时间:2018-12-21 10:23:17

标签: node.js mocha data-driven-tests parameterized-tests

我的mocha规范的定义方式是描述具有一个全局数组(声明为空),该数组在我的第一个it中进行访问和修改。第二个测试是itParam(请参阅mocha-param)测试,该测试尝试访问它,但发现它为空,因此itParam测试根本不运行。

示例(it可以访问修改后的全局值):

describe('some test', function () {
    var arr = [];
    it('should pass a value', function (done) {
        arr.push(5);
        done();
    });
    it('and then double it', function (done) {
        console.log(arr); // [5]
        done();
    });
});

示例(itParam无法访问已修改的全局值):

let itParam = require('mocha-param');

describe('some test', function () {
    var arr = [];
    it('should pass a value', function (done) {
        arr.push(5);
        done();
    });
    itParam('and then double it', arr, function (done, v) {
        console.log(v); // []
        done();
    });
});

PS:我了解,不建议在一种测试中修改值然后在另一项测试中使用它的做法,但这对我来说是不可避免的。

0 个答案:

没有答案