Тест не пройден, когда я имитирую throwError () - PullRequest
0 голосов
/ 24 мая 2019

Я пытался протестировать «случай ошибки» метода, но я думаю, что если я имитирую метод, этот макет влияет на все остальные мои тесты.

Поэтому я пытаюсь отделить случай успехаи описан случай ошибки моего метода в 2.

Когда я запускаю покрытие тестов, случай ошибки покрывается, но тест в консоли завершается неудачей ...

https://image.noelshack.com/fichiers/2019/21/5/1558683399-test1.png

Консоль MSG:

https://image.noelshack.com/fichiers/2019/21/5/1558683502-test2.png

Это метод:

getAgreementSelector() {
this.isLoading = true;

this.frameworkAgreements$ = this.entityManager.getEntity(AgreementSelectorEntity, {})
  .pipe(
    take(1),
    map((dataEntity: any) => dataEntity.value),
    filter((data: Array<IFrameworkAgreement>) => !!data && data.length > 0));

this.frameworkAgreements$.subscribe((data: Array<IFrameworkAgreement>) => {
  this.isLoading = false;
  this.agreementValue = data;
  this.error = false;
  const sessionStorageFAId = sessionStorage.getItem('selectedAgreementId');
  if (sessionStorageFAId) {
    this.onCustomerSelected(sessionStorageFAId);
  } else {
    this.onCustomerSelected(this.agreementValue[0].id.toString());
  }
  for (let j = 0; j < this.agreementValue.length; j++) {
    if (this.agreementValue[j].id.toString() === sessionStorageFAId) {
      this.selectedFrameworkAgreement = this.agreementValue[j].id.toString();
    }
  }
}, (error: any) => {
  this.isLoading = false;
  if (error.status === 404 || error.status === 500 || error.status === 403) {
    this.hideErrorFlag = true;
    this.error = true;
  }
});

}

И тест:

it('should set error variables if there is an error (status 404)', () => {

    // arrange
    UtilsTest.mockReturnValue(component['entityManager'], 'getEntity', throwError({ status: 404 }));

    // act
    component.getAgreements();

    component.agreements$.subscribe(() => {
      // assert
      expect(component.isLoadingDetails).toBeFalsy();
      expect(component.hideErrorFlag).toBeTruthy();
      expect(component.errorDetails).toBeTruthy();
    });

  }
);

Спасибо!

1 Ответ

0 голосов
/ 24 мая 2019

Попробуй вот так:

it('should set error variables if there is an error (status 404)', async(() => {
  spyOn(component['entityManager'], 'getEntity').and.throwError({ status: 404 });

  try {
    await component.getAgreements();
  } catch (e) {
    expect(component.isLoadingDetails).toBeFalsy();
    expect(component.hideErrorFlag).toBeTruthy();
    expect(component.errorDetails).toBeTruthy();
  }
}));
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...