In component:
private xxxService: XxxService,
const xxxList = await this.xxxService.getSomething(this.someid).toPromise();
In testing file component.spec.ts:
import {Observable, of} from 'rxjs';
const xxxServiceStub = {
getSomething: () => Promise.resolve([])
};
beforeEach((async(next) => {
await TestBed.configureTestingModule({
declarations: [someComponent],
imports: [
....
],
providers: [
{provide: XxxService, useValue: xxxServiceStub},
]
}).compileComponents();
next();
}));
API:
public getSomething(someid): Observable<xxxRef[]> {
return this.httpClient
.get<xxxRef[]>(
${environment.url}/api/xxx?someid=${someid},
{
observe: 'response'
}
).pipe(
map(response => {
return response.body;
})
);
}
Interface:
export interface xxxRef {
name: string;
id: number;
description?: string;
}
Test goes, but there is a mistake in console of Karma:
zone-evergreen.js:798 Uncaught Error: Uncaught (in promise): TypeError: this.xxxService.getSomething(...).toPromise is not a function