Почему покрытие филиалов является случайным? - PullRequest
0 голосов
/ 28 января 2020

У меня есть 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

Снимок экрана:

enter image description here

Только последний параметр 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",

Итак, вопрос: почему покрытие филиалов выглядит случайным или я что-то пропустил в своем коде? Должен ли я выполнить тест или это не важно?

1 Ответ

1 голос
/ 28 января 2020

Единственный раз, когда вы вызываете методы, предоставляющие опции, это вызов удаления (где, я подозреваю, вы на самом деле не имеете в виду).

...