I'm trying to do a pretty simple test in Angular with RxJs Observables, but I'm coming up short. This is what I'm basically trying to test:
// We're inside some Angular component here...
let testMe = 0;
function somethingOrOther(): void {
this.someService.methodReturningObservable()
.subscribe(
(nextValue: number): void => {
testMe += nextValue;
}
)
}
How do I test that testMe
gets updated properly when the observable behind methodReturningObservable
emits a value?
I attempted it with this:
it(`works 'cuz I want it to`, fakeAsync(() => {
spyOn(this.someService, 'methodReturningObservable').and.returnValue(cold('a', {a: 10}));
tick();
expect(component.testMe).toBe(10);
}));
So, tick()
doesn't seem to do anything here. Nothing gets my cold
value to ever emit on my spy.
I tried getTestScheduler.flush()
instead as shown at https://netbasal.com/testing-observables-in-angular-a2dbbfaf5329 under the marbles section.
Can I use marbles like this to emit a value on an observable? This was very easy in AngularJS by just triggering a digest, but I can't seem to get Anguar to let me in the next callback for an observable.