如何测试和到达管道内的代码部分
How to test and reach the part of code inside a pipe
我的服务看起来不像这样
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();
抛出一个从未调用过间谍的错误,我怎样才能到达这部分代码?
因为 service.getData()
returns 和 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();
}));
我的服务看起来不像这样
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();
抛出一个从未调用过间谍的错误,我怎样才能到达这部分代码?
因为 service.getData()
returns 和 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();
}));