Angular Jasmine 这个服务方法不是函数

Angular Jasmine this service method is a not a function

我试图让一个服务方法在单元测试中通过,但它总是失败:

“类型错误: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();
  })
);

这是有问题的服务:

  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 
      })
    );
  }

我的测试有提供者内部的方法:

    { provide: EventsService, useValue: { getEvent: () => of([])}},
    { provide: EventsService, useValue: { getEventV2: () => of([])}},

问题是 Angular 正在使用您为 EventsService 提供的最后一个 provider

要修复它,请执行以下操作:

{ provide: EventsService, useValue: { getEvent: () => of([]), getEventV2: () => of([]) } },
// remove the two instances of EventsService and keep it to one.