Skip to content

Assertion

一句话解释

assertion 是 '检查' 部分的 测试 — 它验证那个实际 result matches 预期结果.

用大白话说

assertion 是核心的 测试. 它's 语句那个 says 'I expect 这个到 是真.' 如果它's 真, 测试通过. 如果它's 假, 测试失败.

For 示例, expect(result).toBe(5) 是 assertion. 它检查那个 result equals 5. 如果 result 是 5, assertion 通过. 如果 result 是任何事 else, assertion 失败 and 测试是 marked as 失败.

Different 类型的 assertions 检查不同 things: equality (toBe), deep equality (toEqual), truthiness (toBeTruthy), 数组 membership (toContain), whether 函数 throws (toThrow), and 许多更多.

示例

javascript
expect(2 + 2).toBe(4);        // equality
expect([1, 2]).toContain(2);  // membership
expect(null).toBeNull();      // null check
expect(() => throwErr()).toThrow(); // throws

每个行 是不同类型的 assertion 检查不同条件.

相关术语

相关资源

相关课程

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表