我无法创建一个测试来测试在startTimer函数中观察到的计时器。我使用的是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();
}
}发布于 2018-09-14 00:51:23
我使用以下方法来解决问题:
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();
});
});https://stackoverflow.com/questions/52302066
复制相似问题