首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在jasmine-marble中创建一个可观察的计时器测试?

如何在jasmine-marble中创建一个可观察的计时器测试?
EN

Stack Overflow用户
提问于 2018-09-13 03:23:51
回答 1查看 684关注 0票数 0

我无法创建一个测试来测试在startTimer函数中观察到的计时器。我使用的是6.3.1版本的RXJS。

这是我的代码:

代码语言:javascript
复制
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();
  }
}
EN

回答 1

Stack Overflow用户

发布于 2018-09-14 00:51:23

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

代码语言:javascript
复制
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();
  });
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52302066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档