簡體   English   中英

如何在茉莉花大理石中創建可觀察的計時器測試?

[英]How to create an Observable timer test in jasmine-marble?

我無法創建測試以測試功能startTimer中的Observable計時器。 我使用的是RXJS的6.3.1版本。

這是我的代碼:

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();
  }
}

我使用以下方法解決了我的問題:

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();
  });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM