不能为 QUnit 使用参数化插件
Cannot use parametrize plugin for QUnit
不能运行一个简单的参数化测试:
qunit.cases([{a: 1, b: 2}]).test("my test", function (params, assert) {
var sum = params.a + params.b;
assert.equal(3, sum, "correct");
});
说回调函数为空,其实不是
根据 parameterize.js 文档:parameterize plugin for qunit
我应该这样做:
QUnit
.cases(testCasesList)
.test(title, [expect], callback);
我检查了代码,如果 expect 为 null,则第二个它被初始化为回调函数。我什至尝试将 expect = 3 和回调作为第三个参数,但它仍然给出相同的错误,回调函数为空。
我可能做错了什么?
期望参数是什么,一个函数一个数字?
我想这个工具是为以前版本的 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);
但未能如愿。我所做的是,我将 "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"参数。不确定此 "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 中删除了异步。
感谢阅读:)
不能运行一个简单的参数化测试:
qunit.cases([{a: 1, b: 2}]).test("my test", function (params, assert) {
var sum = params.a + params.b;
assert.equal(3, sum, "correct");
});
说回调函数为空,其实不是
根据 parameterize.js 文档:parameterize plugin for qunit
我应该这样做:
QUnit
.cases(testCasesList)
.test(title, [expect], callback);
我检查了代码,如果 expect 为 null,则第二个它被初始化为回调函数。我什至尝试将 expect = 3 和回调作为第三个参数,但它仍然给出相同的错误,回调函数为空。
我可能做错了什么?
期望参数是什么,一个函数一个数字?
我想这个工具是为以前版本的 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);
但未能如愿。我所做的是,我将 "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"参数。不确定此 "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 中删除了异步。 感谢阅读:)