【发布时间】:2018-04-04 20:52:50
【问题描述】:
我有以下 sn-p 代码,它是在浏览器中运行良好的组件的一部分。但是当我运行我的 Karma/Jasmine 单元测试时,它失败了。
代码:
const request = this.queryQueue.pipe(switchMap(() => this.loadAppointments().catch(e => {
return new Array<Appointment>();
})));
request.subscribe(
newAppointments=> {
this.appointments = this.myData.concat(newAppointments);
}
);
注意事项:
- queryQueue - 是一个主题
- loadAppointments() - 执行 Web 服务请求并返回 Observable
加载约会
loadAppointments(): Observable<Appointment[]> {
this.isLoading = true;
return this.webApiService.getAppointments(this.getSearchDto(), this.appointments.length, 20);
}
目标:
- 可观察管道由 queryQueue 对象启动
- 从服务器请求数据并附加到当前数据列表中
- 阻止长时间运行的请求在后续请求之后返回(因此是 switchMap)
- 在 catch 中处理错误
错误:
TypeError: _this.loadAppointments(...).catch is not a function
at SwitchMapSubscriber.eval [as project] (webpack:///./src/app/components/appointments/appointments.component.ts?:72:117)
at SwitchMapSubscriber._next (webpack:///./node_modules/rxjs/_esm5/operators/switchMap.js?:94:27)
at SwitchMapSubscriber.Subscriber.next (webpack:///./node_modules/rxjs/_esm5/Subscriber.js?:97:18)
at Subject.next (webpack:///./node_modules/rxjs/_esm5/Subject.js?:66:25)
at AppointmentsComponent.refresh (webpack:///./src/app/components/appointments/appointments.component.ts?:84:25)
at AppointmentsComponent.set [as day] (webpack:///./src/app/components/appointments/appointments.component.ts?:42:18)
at AppointmentsComponent.gotoToday (webpack:///./src/app/components/appointments/appointments.component.ts?:111:18)
at AppointmentsComponent.ngOnInit (webpack:///./src/app/components/appointments/appointments.component.ts?:64:14)
at checkAndUpdateDirectiveInline (webpack:///./node_modules/@angular/core/esm5/core.js?:12585:19)
at checkAndUpdateNodeInline (webpack:///./node_modules/@angular/core/esm5/core.js?:14109:20)
单元测试:
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AppointmentsComponent
],
imports: [
InfiniteScrollModule,
FormsModule,
BrowserAnimationsModule
],
providers: [
{ provide: WebApiService, useClass: MockWebApiService }
]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AppointmentsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
/* ...Code missing for brevity... */
class MockWebApiService {
getAppointments(): Observable<Appointment[]> { return of([]);}
}
【问题讨论】:
标签: angular rxjs karma-jasmine