在jQuery QUnit中如何从“equal()”断言中获取断言结果?

时间:2012-04-21 06:13:29

标签: jquery qunit

我有一些与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;
     }
});

1 个答案:

答案 0 :(得分:2)

只需将其分配给变量并检查即可。

var result = 0 < result && result < anyNumber + 1;

equal(result, true);

if ( ! result) {
    break;
}