温馨提示:本文翻译自stackoverflow.com,查看原文请点击:node.js - Rxjs of operator doesn't emit value when using proxies
node.js rxjs typescript typemoq

node.js - 使用代理时,运算符的Rxjs不会发出值

发布于 2020-04-10 23:46:14

我有一个使用Substitute.js的简单单元测试(也使用TypeMoq模拟进行了测试,我将描述的行为是相同的)。

在此测试中,我尝试使用of操作符来发出模拟对象,这是最简单的事情没有任何其他运算符,就不会调用订阅回调。范例:

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

永远不会调用控制台日志。

现在,如果我不使用运算符,而只是创建一个可观察对象,则该日志正在运行。范例:

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

of在这种情况下,有关操作员情况的任何线索

我在用着 :

  • rxjs:6.5.4
  • 打字稿:3.5.3
  • 节点:v10.18.1

查看更多

提问者
Nick Tsitlakidis
被浏览
38
martin 2020-02-02 04:59

of()方法有一个特殊的用例,您将RxJS的实例作为最后一个参数传递Scheduler(该用例已弃用,并将在RxJS 8中删除)。 https://github.com/ReactiveX/rxjs/blob/master/src/internal/observable/of.ts#L9

例如,您可以执行以下操作:

of(1, 2, 3, asyncScheduler)

由于Substitute.for<MessageFactory>()返回function(try typeof factoryof()认为您正在传递调度程序。Substitute.for实际上,返回时Proxy会混淆RxJS检查因此,出于这个原因,它从不发出任何东西。

无论如何,您可以这样做:

from([factory]).subscribe((f) => console.log("got it"));