Cor*_*cor 2 javascript jasmine typescript angular
我试图让一个服务方法在单元测试中通过,但是它一直失败:
“类型错误:this.eventsService.getEvent 不是函数”
我的组件内的代码:
constructor(
private eventsService: EventsService
)
this.subscriptions.add(
this.eventsService.getEvent().subscribe(events => {
this.events = events;
this.soSomeMethod();
})
);
// Also have a V2 method, unsure if it matters but adding it for clarity
this.subscriptions.add(
this.eventsService.getEventV2().subscribe(eventsV2 => {
this.eventsV2 = eventsV2;
this.soSomeMethod();
})
);
Run Code Online (Sandbox Code Playgroud)
这是有问题的服务:
public getEvent(): Observable<event[]> {
return combineLatest(this.store.select(getEventStore), this.store.select(getEvents)).pipe(
map(state => {
// do something
})
);
}
public getEventV2(): Observable<event[]> {
return combineLatest(this.store.select(getEventStore), this.store.select(getEvents)).pipe(
map(state => {
// do something
})
);
}
Run Code Online (Sandbox Code Playgroud)
我的测试有提供者内部的方法:
{ provide: EventsService, useValue: { getEvent: () => of([])}},
{ provide: EventsService, useValue: { getEventV2: () => of([])}},
Run Code Online (Sandbox Code Playgroud)
问题是 Angular 正在使用provider您提供的最后一个EventsService.
要修复它,请执行以下操作:
{ provide: EventsService, useValue: { getEvent: () => of([]), getEventV2: () => of([]) } },
// remove the two instances of EventsService and keep it to one.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8799 次 |
| 最近记录: |