我有一些与Math.floor((Math.random() * 9) + 1);
一起使用的函数,然后用我使用for
的其余逻辑来测试它们,如:
test("getRandomNumber(anyNumber) should return a number between 1..anyNumber", function () {
var anyNumber, result;
for (var i = 0; i <= 100; i++) {
anyNumber = Math.floor((Math.random() * 9) + 1);
result = getRandomNumber(anyNumber);
equal((0 < result && result < (anyNumber + 1)), true);
}
});
我想从equal
断言获得结果,如果一个断言失败,则不需要保持循环,如:
test("getRandomNumber(anyNumber) should return a number between 1..anyNumber", function () {
var anyNumber, result;
for (var i = 0; i <= 100; i++) {
anyNumber = Math.floor((Math.random() * 9) + 1);
result = getRandomNumber(anyNumber);
if (!equal((0 < result && result < (anyNumber + 1)), true));
break;
}
});
答案 0 :(得分:2)
只需将其分配给变量并检查即可。
var result = 0 < result && result < anyNumber + 1;
equal(result, true);
if ( ! result) {
break;
}