gpt4 book ai didi

javascript - 无法使用 QUnit 的参数化插件

转载 作者:行者123 更新时间:2023-11-27 22:36:58 24 4
gpt4 key购买 nike

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

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

assert.equal(3, sum, "correct");
});

它说回调函数为 null,但事实并非如此

qunit parameterized test

根据parameterize.js文档:parameterize plugin for qunit

我应该这样做:

QUnit
.cases(testCasesList)
.test(title, [expect], callback);

我检查了代码,如果expect为null,那么它就会被初始化为回调函数。我什至尝试了expect = 3和callback作为第三个参数,但它仍然给出相同的错误,回调函数为空。

我可能做错了什么?

应该期待什么参数,一个函数,一个数字?

最佳答案

我猜这个工具是为以前版本的 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;
}
};
}())
});

首先,我猜作者想用 4 个附加功能来扩展 QUnit:

sequential(addData);
combinatorial(mixData);
test(title, expected, callback);
asyncTest(title, expected, callback);

但未能做到这一点。我所做的是,我将“案例”转换为对象而不是函数,并添加了 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() 和 Combinatorial() 参数段中创建测试组合

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 2.x upgrade我从 QUnit Parameteize 中删除了异步。感谢您的阅读:)

关于javascript - 无法使用 QUnit 的参数化插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39017452/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com