deepthan / blog-angular

Angular 笔记
280 stars 58 forks source link

【Angular】单元测试方法 #142

Open deepthan opened 3 years ago

deepthan commented 3 years ago

单元测试 一般都是给通用组件,指令 服务 管道这样的通用业务去写 页面级就用e2e测试

语句

toBe() 等同 ===
toNotBe() 等同 !==
toBeDefined() 等同 !== undefined
toBeUndefined() 等同 === undefined
toBeNull() 等同 === null
toBeTruthy() 等同 !!obj
toBeFalsy() 等同 !obj
toBeLessThan() 等同 <
toBeGreaterThan() 等同 >
toEqual() 相当于 ==
toNotEqual() 相当于 !=
toContain() 相当于 indexOf
toBeCloseTo() 数值比较时定义精度,先四舍五入后再比较。
toHaveBeenCalled() 检查function是否被调用过
toHaveBeenCalledWith() 检查传入参数是否被作为参数调用过
toMatch() 等同 new RegExp().test()
toNotMatch() 等同 !new RegExp().test()
toThrow() 检查function是否会抛出一个错误

Suites 和 Specs 分别可以用 xdescribe 和 xit 全局函数来跳过这些测试代码块。

写单元测试 顺序

- utils 纯函数
- pipe
- class
- service
- directive
- component