I have a simple unit test that uses Substitute.js (also tried this with TypeMoq mocks and the behavior I'll describe is the same).
In this test I try the simplest thing, using the of operator to emit the mocked object. Without any further operators, the subscribe callback is never called. Example :
import {Arg, Substitute, SubstituteOf} from "@fluffy-spoon/substitute";
import "reflect-metadata";
import {Observable, of} from "rxjs";
const factory = Substitute.for<MessageFactory>();
of(factory).subscribe((f) => console.log("got it"));
The console log is never called.
Now, if I don't use the operator and I just create an observable, the log is working. Example :
import {Arg, Substitute, SubstituteOf} from "@fluffy-spoon/substitute";
import "reflect-metadata";
import {Observable, of} from "rxjs";
const factory = Substitute.for<MessageFactory>();
new Observable((subscriber) => {
subscriber.next(factory);
subscriber.complete();
}).subscribe((f) => console.log("got it"));
Any clues on what's going on with the of operator in this case?
I'm using :
- rxjs: 6.5.4
- typescript: 3.5.3
- node : v10.18.1
of()method has one special use-case where you pass as the last argument an instance of RxJSScheduler(This use-case is deprecated and will be removed in RxJS 8). https://github.com/ReactiveX/rxjs/blob/master/src/internal/observable/of.ts#L9For example you could do the following:
Since
Substitute.for<MessageFactory>()returns afunction(trytypeof factory)of()thinks you're passing a scheduler.Substitute.forin fact returnsProxyunder the hood which confuses RxJS check. So for this reason it never emits anything.Anyway, you can do for example this: