Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
155
rated 0 times [  162] [ 7]  / answers: 1 / hits: 5468  / 3 Years ago, wed, january 13, 2021, 12:00:00

I am trying to get a service method to pass inside a unit test, however it keeps failing:


"TypeError: this.eventsService.getEvent is not a function"


My code inside my component:


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

This is the service in question:


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

My testing has the methods inside the providers:


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

More From » angular

 Answers
2

The problem is Angular is using the last provider you have provided for EventsService.


To fix it, do this:


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

[#1961] Sunday, January 10, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tobyl

Total Points: 598
Total Questions: 110
Total Answers: 114

Location: Vietnam
Member since Sat, Feb 12, 2022
2 Years ago
tobyl questions
;