【问题标题】:fakeAsync does not work with debounceTimefakeAsync 不适用于 debounceTime
【发布时间】:2019-10-07 15:48:11
【问题描述】:

我正在尝试使用debounceTime (rxjs) 为Angular 应用程序中的功能编写单元测试。并使用fakeAsync 进行异步测试。 即使我没有设置tick() 或设置像tick(500) 这样的小间隔,它看起来在测试中debounceTime 也会立即得到解决。

例如使用 delay(1000) 而不是 debounceTime(1000) fakeAsync 可以正常工作。

测试

describe('rr', () => {
    it('should get Date diff correctly in fakeAsync with rxjs scheduler', fakeAsync(() => {
        let result = null;
        of ('hello').pipe(debounceTime(1000)).subscribe(v => { result = v; });
        expect(result).toBeNull(); // But it is 'Hello' - debounceTime resolves immediately
        tick(1000);
        expect(result).toBe('hello');
...
  }));
})

stackblitzhttps://stackblitz.com/edit/angular-ohhi9e?file=src%2Fapp%2Fapp.component.spec.ts

【问题讨论】:

    标签: angular rxjs jasmine debounce


    【解决方案1】:

    of 运算符在收到通知后立即完成。如果不需要额外的通知,debounceTime 不需要等待,因此会在发出完整通知的那一刻通知。

    要实现您的结果,请尝试使用像 Subject 这样的长期可观察对象。

    describe('rr', () => {
        it('should get Date diff correctly in fakeAsync with rxjs scheduler', fakeAsync(() => {
            let result = null;
            new BehaviourSubject ('hello').pipe(debounceTime(1000)).subscribe(v => { result = v; });
            expect(result).toBeNull();
            tick(1000);
            expect(result).toBe('hello');
    ...
      }));
    })
    

    来源:debounceTime on GitHub

    _complete() { this.debouncedNext(); ... }

    【讨论】:

      猜你喜欢
      • 2019-07-03
      • 1970-01-01
      • 1970-01-01
      • 2019-06-06
      • 1970-01-01
      • 2022-12-21
      • 1970-01-01
      • 1970-01-01
      • 2017-02-15
      相关资源
      最近更新 更多