Я пишу жасминовый тест для вызова метода в 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();
});