如果你有 JavaScript 1.6,你可以使用Array.indexOf
test("myFunction with expected value", function() {
var expectedValues = ['abc','cde','efg','mgh'];
ok(expectedValues.indexOf(myFunction()) !== -1, 'myFunction() should return an expected value');
});
如果你愿意,你可以扩展 QUnit 来支持这些断言:
QUnit.extend(QUnit, {
inArray: function (actual, expectedValues, message) {
ok(expectedValues.indexOf(actual) !== -1, message);
}
});
然后你可以在你的测试中使用这个自定义的inArray() 方法:
test("myFunction with expected value", function() {
var expectedValues = ['abc','cde','efg','mgh'];
QUnit.inArray(myFunction(), expectedValues, 'myFunction() should return an expected value');
});
我创建了a jsFiddle to show both options。