1
votes

I am trying to stub API and thus I would like to return stubbed response from my Service.

My service method looks like:

public addItem(item): Observable<void> {
    this.listOfItems.push(item);
    return of();
}

And my component method looks like:

public submit(): void {
   this.service.addItem(this.item)
       .subscribe(() => console.log('working'));
}

Unfortunately this way subscribe is not entered and nothing is displayed in console.

How can I return empty Observable<void> that could be subscribed?

1

1 Answers

1
votes

void means nothing, try to use undefined.

return of(undefined);

otherwise you can always use the ugliest any hack, but for tests it is acceptable, to avoid complexity during recreation of properly typed mocks and save some time with a fast any cast IMO.

return <any>of(true);