【问题标题】:How to create an Observable timer test in jasmine-marble?如何在 jasmine-marble 中创建 Observable 计时器测试?
【发布时间】:2018-09-12 19:23:50
【问题描述】:

我无法创建测试来测试函数 startTimer 中的计时器 Observable。我使用的是 6.3.1 版本的 RXJS。

这是我的代码:

private createSubscribe(): void {
  if (this.timer) {
    this.subscription = this.timer.subscribe(() => {
      this.cream().then(() => {
        this.subscription.unsubscribe();
        this.subscription = null;
        this.timer = null;
        this.startTimer(this.config.period);
      });
    });
  }
}

private startTimer(period): void {
  if (period) {
    this.timer = timer(period * 1000);
    this.createSubscribe();
  }
}

【问题讨论】:

    标签: javascript angular typescript jasmine


    【解决方案1】:

    我使用以下方法解决了我的问题:

    it('startTimer: should call createSubscribe and set timer with observable emited in 5000ms.', () => {
    
      const scheduler = new TestScheduler((actual, expected) => {
        expect(actual).toEqual(expected);
      });
    
      scheduler.run(helpers => {
        const { expectObservable } = helpers;
        const period = 5;
        const expected = '5000ms (a|)';
        spyOn(helper, <any>'createSubscribe');
    
        helper['startTimer'](period);
    
        expectObservable(helper['timer']).toBe(expected, {a : 0});
        expect(helper['createSubscribe']).toHaveBeenCalled();
      });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-17
      • 2018-06-09
      • 2019-09-06
      • 1970-01-01
      • 2021-09-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多