0

使用此代码:

select(): void {
        this.initialObservable$
            .pipe(
                first(),
                switchMap(() => this.service.getData())
            ).subscribe(// do stuff);
}

我刚刚遇到了内存泄漏错误。稍后再次发出服务调用,我意识到订阅从未清理过,因为订阅内容再次运行。

显然这是我的修复:

select(): void {
        this.initialObservable$
            .pipe(
               switchMap(() => this.service.getData()),
                first()
            ).subscribe(// do stuff);
}

现在工作正常 - 将first()操作员调用移动到管道的末端。

我一直在研究 jasmine marble 测试,我相信我需要参考可观察流来测试它,而我没有。所以回归测试会很混乱,这让我觉得我在做的事情一定是不好的做法。请问以可单元测试的方式编写此类代码的正确方法是什么?最好用于大理石测试。

4

1 回答 1

0

如果您在此处返回该 observable 并在调用者中订阅,那么这将很容易测试。

scheduler.run(({ cold, expectObservable }) => {
  const source$ = cold('      -n--y--', { n: false, y: true });
  const expectedMarble = '    ----x';
  const result$ = select();
  
  expectObservable(result$).toBe(expectedMarble, { x: true });
});
于 2020-08-27T14:41:25.670 回答