为什么分支覆盖是随机的?
Why branches coverage is random?
我有这 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, body: object, options: object = this.httpOptions): Promise<T> {
delete<T>(url: string, options: object = this.httpOptions): Promise<T> {
结果:
75% 分支 3/4
截图:
只有最后一个参数 options
没有被覆盖,前 3 个被覆盖。
图书馆:
"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",
所以问题是:为什么分支覆盖看起来是随机的或者我的代码中遗漏了一些东西?我应该 运行 测试还是不重要?
您唯一一次调用提供选项的方法是在调用 delete 时(我怀疑您实际上并不是故意的)。
我有这 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, body: object, options: object = this.httpOptions): Promise<T> {
delete<T>(url: string, options: object = this.httpOptions): Promise<T> {
结果:
75% 分支 3/4
截图:
只有最后一个参数 options
没有被覆盖,前 3 个被覆盖。
图书馆:
"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",
所以问题是:为什么分支覆盖看起来是随机的或者我的代码中遗漏了一些东西?我应该 运行 测试还是不重要?
您唯一一次调用提供选项的方法是在调用 delete 时(我怀疑您实际上并不是故意的)。