As mentioned here if you use of
from rxjs
to implement mock services you can get some unexpected results. In my case, my code always cleans up its subscriptions. But, by default, of
executes synchronously, so the following code will fail because sub
isn’t set when the closure is called.
import { of } from 'rxjs';
const mockService = {
get() {
return of([]);
}
}
const sub = mockService.get()
.subscribe(result => {
this.list = result;
sub.unsubscribe();
});
In order to get this code to behave like a the real service we need to use a scheduler.
import { of, asyncScheduler } from 'rxjs';
const mockService = {
get() {
return of([], asyncScheduler);
}
}
const sub = mockService.get()
.subscribe(result => {
this.list = result;
sub.unsubscribe();
});