assert.expect()
添加版本:1.0.0.
描述
expect( amount )
指定测试中预期的断言数量。
名称 | 描述 |
---|---|
amount |
此测试中预期的断言数量 |
这最常被用作 assert.expect(0)
,表示测试可以在不进行任何断言的情况下通过。这意味着测试仅用于验证代码是否运行完成,没有任何未捕获的错误。这本质上是 assert.throws()
的反面。
这也可以用于明确要求在给定测试中记录一定数量的断言。如果之后断言的数量与预期数量不匹配,则测试将失败。
建议使用 assert.step()
或 assert.async()
来测试异步代码。
示例
示例:无断言
没有断言的测试
QUnit.test('example', function (assert) {
assert.expect(0);
var android = new Robot();
android.up(2);
android.down(2);
android.left();
android.right();
android.left();
android.right();
android.attack();
android.jump();
});
示例:自定义断言
如果您使用的是在期望不满足时抛出异常的通用断言库,则如果测试中不需要其他断言,可以使用 assert.expect(0)
。
QUnit.test('example', function (assert) {
assert.expect(0);
var android = new Robot(database);
android.run();
database.assertNoOpenConnections();
});
示例:显式计数
要求显式断言计数。
QUnit.test('example', function (assert) {
assert.expect(2);
function calc (x, operation) {
return operation(x);
}
let result = calc(2, function (x) {
assert.true(true, 'calc() calls operation function');
return x * x;
});
assert.strictEqual(result, 4, '2 squared equals 4');
});