我的服務看起來不像這樣
getData(properties: any): Observable<any[]> {
const qrList$: Observable<any[]> =
properties.type === 'e'
? this.getByE(properties)
: this.getByD(properties);
return qrList$.pipe(
map((qrList) => this.filter(qrList));
}
private getByE(properties: any): Observable<any[]> {
let request$: Observable<any[]>;
if (properties.o === 'c') {
request$ = this.qrService.getQ();
} else {
request$ = this.eService.getEncByE()
.pipe(
map((encList: any[]) => this.getFinished(encList)),
switchMap((list) => {
return previousEncounterIdList?.length > 0
? this.qrService.getQ()
: of([]);
}),
);
}
return request$;
}
和我的規格檔案
...
beforeEach(() => {
spyOn(eService, 'getEncByE').and.returnValue(of([{}]));
spyOn(qrService, 'getQ').and.returnValue(of([]));
});
...
it('whatever', fakeAsync(() => {
propertiesMock.type = 'e';
propertiesMock.o = 'p';
service.getData(propertiesMock);
tick();
expect(eService.getEncByE).toHaveBeenCalled();
expect(qrService.getQ).toHaveBeenCalled();
}));
最后一行代碼expect(qrService.getQ).toHaveBeenCalled();拋出了一個從未呼叫過間諜的錯誤,我怎樣才能到達這部分代碼?
uj5u.com熱心網友回復:
由于service.getData()回傳一個Observable,您必須subscribe訂閱它才能“起飛”。
嘗試這個:
it('whatever', fakeAsync(() => {
propertiesMock.type = 'e';
propertiesMock.o = 'p';
// mock getEncByE
spyOn(eService, 'getEncByE').and.returnValue(of([]));
// have to maybe do some more mocking as well
service.getData(propertiesMock).subscribe(() => {
expect(eService.getEncByE).toHaveBeenCalled();
expect(qrService.getQ).toHaveBeenCalled();
});
tick();
}));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/440121.html
上一篇:模擬時單元測驗協程空指標例外
