Javascript 无法对QUnit使用参数化插件

Javascript 无法对QUnit使用参数化插件,javascript,unit-testing,parameters,qunit,Javascript,Unit Testing,Parameters,Qunit,无法运行简单的参数化测试: qunit.cases([{a: 1, b: 2}]).test("my test", function (params, assert) { var sum = params.a + params.b; assert.equal(3, sum, "correct"); }); 它说回调函数为null,但不是 根据parameterize.js文档: 我应该这样做: QUnit .cases(testCasesList) .test(title,

无法运行简单的参数化测试:

qunit.cases([{a: 1, b: 2}]).test("my test", function (params, assert) {
    var sum = params.a + params.b;

    assert.equal(3, sum, "correct");
});
它说回调函数为null,但不是

根据parameterize.js文档:

我应该这样做:

QUnit
.cases(testCasesList)
.test(title, [expect], callback);
我检查了代码,如果expect为null,那么第二步它将初始化为回调函数。我甚至尝试将expect=3和callback作为第三个参数,但它仍然给出相同的错误,callback函数为null

我可能做错了什么


参数、函数和数字应该是什么?

我猜这个工具是为以前版本的QUnit制作的。我进行了一些调试,找到了一个解决方案:

/*
 * Parameterize v 0.4
 * A QUnit Addon For Running Parameterized Tests
 * https://github.com/AStepaniuk/qunit-parameterize
 * Released under the MIT license.
 */
QUnit.extend(QUnit, {
    cases: (function() {
    'use strict';
    var currentCases = null,
        clone = function(testCase) {
            var result = {},
                p = null;

            for (p in testCase) {
                if (testCase.hasOwnProperty(p)) {
                    result[p] = testCase[p];
                }
            }

            return result;
        },
        createTest = function(methodName, title, expected, callback, parameters) {

            QUnit[methodName](title + ", test params: " + JSON.stringify(parameters), function(assert) {
                return callback.call(this, parameters, assert);
            });
        },

        iterateTestCases = function(methodName, title, expected, callback) {
            var i = 0,
                parameters = null,
                testCaseTitle = null;

            if (!currentCases || currentCases.length === 0) {
                // setup test which will always fail
                QUnit.test(title, function(assert) {
                    assert.ok(false, "No test cases are provided");
                });
                return;
            }

            if (!callback) {
                callback = expected;
                expected = null;
            }

            for (i = 0; i < currentCases.length; i += 1) {
                parameters = currentCases[i];

                testCaseTitle = title;
                if (parameters.title) {
                    testCaseTitle += "[" + parameters.title + "]";
                }

                createTest(methodName, testCaseTitle, expected, callback, parameters);
            }
        },

        getLength = function(arr) {
            return arr ? arr.length : 0;
        },

        getItem = function(arr, idx) {
            return arr ? arr[idx] : undefined;
        },

        mix = function(testCase, mixData) {
            var result = null,
                p = null;

            if (testCase && mixData) {
                result = clone(testCase);

                for (p in mixData) {
                    if (mixData.hasOwnProperty(p)) {
                        if (p !== "title") {
                            if (!(result.hasOwnProperty(p))) {
                                result[p] = mixData[p];
                            }
                        } else {
                            result[p] = [result[p], mixData[p]].join("");
                        }
                    }
                }

            } else if (testCase) {
                result = testCase;
            } else if (mixData) {
                result = mixData;
            } else {
                // return null or undefined whatever testCase is
                result = testCase;
            }

            return result;
        };

    return {

        init: function(testCasesList) {
            currentCases = testCasesList;
            return this;
        },

        sequential: function(addData) {
            var casesLength = getLength(currentCases),
                addDataLength = getLength(addData),
                length = casesLength > addDataLength ? casesLength : addDataLength,
                newCases = [],
                i = 0,
                currentCaseI = null,
                dataI = null,
                newCase = null;

            for (i = 0; i < length; i += 1) {
                currentCaseI = getItem(currentCases, i);
                dataI = getItem(addData, i);
                newCase = mix(currentCaseI, dataI);

                if (newCase) {
                    newCases.push(newCase);
                }
            }

            currentCases = newCases;

            return this;
        },

        combinatorial: function(mixData) {
            var current = (currentCases && currentCases.length > 0) ? currentCases : [null],
                currentLength = current.length,
                mixDataLength = 0,
                newCases = [],
                i = 0,
                j = 0,
                currentCaseI = null,
                dataJ = null,
                newCase = null;

            mixData = (mixData && mixData.length > 0) ? mixData : [null];
            mixDataLength = mixData.length;

            for (i = 0; i < currentLength; i += 1) {
                for (j = 0; j < mixDataLength; j += 1) {
                    currentCaseI = current[i];
                    dataJ = mixData[j];
                    newCase = mix(currentCaseI, dataJ);

                    if (newCase) {
                        newCases.push(newCase);
                    }
                }
            }

            currentCases = newCases;

            return this;
        },

        test: function(title, expected, callback) {
            iterateTestCases("test", title, expected, callback);
            return this;
        },

        getCurrentTestCases: function () {
            return currentCases;
        }
    };
    }())
});
但没有做到这一点。我所做的是,我将“cases”转换为对象而不是函数,并添加了init()函数,该函数在内部初始化测试用例数组

我还将createTest()函数更改为:

createTest = function(methodName, title, expected, callback, parameters) {
    QUnit[methodName](title + ", test params: " + JSON.stringify(parameters), function(assert) {
        return callback.call(this, parameters, assert);
    });
}
它直接调用QUnit.test(title,callback),而不传递“expected”参数。不确定这个“预期”参数的目的是什么,但是您可以在测试用例数组中添加自己的参数,并且仍然涵盖应该预期的内容

以下是我如何创建3个参数化测试的示例:

QUnit.cases
        .init([{a: 1, b: 2, expected: 3}, {a: 4, b: 5, expected: 9}, {a: -5, b: 5, expected: 0}])
        .test("test sum(a, b)", function (parameters, assert) {
            var sum = parameters.a + parameters.b;

            assert.equal(sum, parameters.expected, "expected: " + parameters.expected + ", calculated: " + sum);
        });
当前脚本包括顺序测试,但我还是保留了此函数:

qunit
    .cases
    .init([
        {param1: 50},
        {param1: 200},
        {param1: 300}
    ])
    .sequential([
        {param2: 100},
        null,
        {param2: 150}
    ])
    .test("Meta tests for QUnit Parametrize plugin", function (params, assert) {
        console.dir(params);
        assert.equal(params.param1, params.param2,"");
    });
您还可以创建组合测试,这将创建init()和combination()参数段中的测试组合

qunit
    .cases
    .init([
        {param1: 50}
    ])
    .combinatorial([
        {param2: 100},
        {param2: 150},
        {param2: 50}
    ])
    .test("Meta tests for QUnit Parametrize plugin", function (params, assert) {
        assert.equal(params.param1, params.param2,"");
    });
最新版本不支持QUnit.async()。您应该使用QUnit.test() 用于异步测试。咨询: 我从QUnit参数化中删除了async。
感谢阅读:)

不知道这东西怎么了,它被窃听了!如果我为expect和callback添加了两个函数,那么只有第一个函数会打印包含QUnit.test对象属性的参数,并且assert未定义:)
qunit
    .cases
    .init([
        {param1: 50}
    ])
    .combinatorial([
        {param2: 100},
        {param2: 150},
        {param2: 50}
    ])
    .test("Meta tests for QUnit Parametrize plugin", function (params, assert) {
        assert.equal(params.param1, params.param2,"");
    });