Ошибка Ожидается, что шпион был вызван, несмотря на то, что метод был обнаружен в тестовом методе. - PullRequest
0 голосов
/ 05 марта 2020

Я пишу жасминовый тест для вызова метода в angular. Я тестирую вызов метода this.calculateRemainingDaysLeft в методе getOutstandingAgreements. В настоящее время я получаю сообщение об ошибке

Expected spy calculateRemainingDaysLeft to have been called.

В своем тесте я попробовал следующее, но ни один из них не помог мне решить проблему

  expect(component.calculateRemainingDaysLeft).toHaveBeenCalled();
 //expect(mockAgreementsService.getOutstandingAgreements).toHaveBeenCalled();

Компонент

 public getOutstandingAgreements(Id: number) {
    this.agreementsService.getOutstandingAgreements(Id).subscribe((data: AgreementsModel[]) => {
         this.myData = data;
         if (this.myData) {
         this.agreementData = this.myData[0].data;
         this.agreementLength = this.myData.length;

         this.calculateRemainingDaysLeft(0);
        }
        });
    }

Тестовый файл

describe('AgreementComponent', () => {
  let component: AgreementComponent;
  let fixture: ComponentFixture<AgreementComponent>;
  let  mockAgreementsService: AgreementsService;


  const mockAgreementsService: any = {
    getOutstandingAgreements(): Observable<AgreementsModel[]> {
        return Observable.of([]);
    },
    updateAgreement: () => Promise.resolve([])
};

  configureTestSuite(() => {
    TestBed.configureTestingModule({
      imports: [SharedModule, FontAwesomeModule],
      declarations: [AgreementComponent, CustomScrollDirective],
      providers: [{ provide: UserService, useValue: mockUserService },
      { provide: AgreementsService, useValue: mockAgreementsService }]
    });
  });  




  fit('should not return data when  getOutstandingAgreements is called', () => {
    const response: AgreementsModel[] = [];
    let outStandingAgreementSpy: jasmine.Spy;
    let outStandingAgreementServiceSpy: jasmine.Spy;


    setupComponent();
    outStandingAgreementSpy = spyOn(component, 'getOutstandingAgreements').and.callThrough();
    outStandingAgreementServiceSpy = spyOn(mockAgreementsService, 'getOutstandingAgreements').and.returnValue( null);
    spyOn(component, 'calculateRemainingDaysLeft').and.callFake(function() {});
    //spyOn(component, 'calculateRemainingDaysLeft').and.callThrough();
    component.ngOnInit();
    expect(outStandingAgreementSpy).toHaveBeenCalled();
    expect(component.calculateRemainingDaysLeft).toHaveBeenCalled();

  });

1 Ответ

0 голосов
/ 05 марта 2020

Измените следующие строки:

outStandingAgreementServiceSpy = spyOn(mockAgreementsService, 'getOutstandingAgreements').and.returnValue( null)

на:

outStandingAgreementServiceSpy = spyOn(mockAgreementsService, 'getOutstandingAgreements').and.returnValue( Observable.of(''))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...