Ich versuche, meinen Service Call in der Komponente mit Testfall wie unten beschrieben zu decken. Aber Fehler bekommen, während ng test
wie unten gezeigt ausgeführt wird:Angular 4 Testfall .then() ist keine Funktion
Failed: this.monthService.getMonthView(...).then is not a function TypeError: this.monthService.getMonthView(...).then is not a function
Service: (MonthService.ts)
getMonthView
ist der Service-Aufruf innerhalb MonthService
getMonthView(forecastMonth: string): Promise<any[]> {
const options = new RequestOptions({ headers: this.headers });
const url = this.BaseUrl + forecastMonth;
return this.http.get(url, options)
.toPromise()
.then(response => response.json() as any[])
.catch(this.handleError);
}
Komponente: (MonthComponent.ts)
MonthService
wird importiert und injiziert in MonthComponent.ts
loadDataOnMonthViewClicked(forecastMonth) {
this.monthService.getMonthView(forecastMonth)
.then(response =>
this.result = response
);
}
Test Suite:
beforeEach(() => {
/**
* Ref:https://angular.io/guide/testing#!#component-fixture
*/
fixture = TestBed.createComponent(MonthComponent);
component = fixture.componentInstance;
myService = TestBed.get(MonthService);
//fixture.detectChanges();
});
it('should be created',() => {
expect(component).toBeTruthy();
});
fit('should test loadDataOnMonthViewClick', async(() => {
let data=["10","20","30"];
spyOn(component.monthService, 'getMonthView').and.returnValue(result);
component.loadDataOnMonthViewClicked('Jan');
fixture.detectChanges();
expect(component.result).toBe(data);
}));