无法运行简单的参数化测试:
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文档:parameterize plugin for qunit
我应该这样做:
QUnit
.cases(testCasesList)
.test(title, [expect], callback);
我检查了代码,如果expect为null,则将其初始化为回调函数。我甚至试过expect = 3和callback作为第三个参数,但它仍然给出相同的错误,回调函数为null。
我可能做错了什么?
应该期待什么参数,一个函数是一个数字?
答案 0 :(得分:1)
我猜这个工具是针对以前版本的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);
但未能这样做。我所做的是,我将“case”转换为object而不是函数,并添加了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()和组合()参数段中创建测试组合
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中删除了异步。 感谢您阅读:)