我有这4个单元测试。
const handleHttp = (method: 'POST' | 'GET' | 'PUT' | 'DELETE'): void => {
const req = httptestingcontroller.expectone(`${environment.apiUrl}test`);
expect(req.request.method).toEqual(method);
return req.flush({ fake: 1 });
};
it('should make a POST request',fakeAsync(() => {
service.post('test',{ fakeProp: 'fakeValue' }).then((res) => {
expect(res).toEqual({ fake: 1 });
});
handleHttp('POST');
}));
it('should make a GET request',fakeAsync(() => {
service.get('test').then((res) => {
expect(res).toEqual({ fake: 1 });
});
handleHttp('GET');
}));
it('should make a PUT request',fakeAsync(() => {
service.put('test',{ fakeProp: 'fakeValue' }).then((res) => {
expect(res).toEqual({ fake: 1 });
});
handleHttp('PUT');
}));
it('should make a DELETE request',fakeAsync(() => {
service.delete('test',{ fakeProp: 'fakeValue' }).then((res) => {
expect(res).toEqual({ fake: 1 });
});
handleHttp('DELETE');
}));
方法选项参数都相同:
post<T>(url: string,body: object,options: object = this.httpoptions): Promise<T> {
get<T>(url: string,options: object = this.httpoptions): Promise<T> {
put<T>(url: string,options: object = this.httpoptions): Promise<T> {
delete<T>(url: string,options: object = this.httpoptions): Promise<T> {
结果:
75%的分支3/4
屏幕截图:
仅不覆盖最后一个参数options
,而覆盖前三个参数。
库:
"jasmine-core": "~3.4.0","jasmine-spec-reporter": "~4.2.1","karma": "~4.1.0","karma-chrome-launcher": "~2.2.0","karma-coverage-istanbul-reporter": "~2.0.1","karma-jasmine": "~2.0.1","karma-jasmine-html-reporter": "^1.4.0",
所以问题:为什么分支覆盖范围看起来是随机的,还是我的代码中缺少某些内容?我应该运行测试还是不重要?