2021-07-06 angular 单元测试

1、常用断言方法

Jasmine 提供非常丰富的API,一些常用的Matchers:
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是否会抛出一个错误

而这些API之前用not 来取反进行判断:
expect(true).not.toBe(false);

2、Angular如何对包含了HTTP请求的服务类进行单元测试

HttpTestingController.expectOne:期望一个基于传入参数url的HTTP请求已经被发起,并且返回其mock
mockReq.flush(mockUsers):使用flush传入的参数作为HTTP请求的返回参数

你可能感兴趣的:(2021-07-06 angular 单元测试)