fakeAsync 不适用于 debounceTime

Ale*_*huk 5 jasmine rxjs debounce angular

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

例如使用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');
...
  }));
})
Run Code Online (Sandbox Code Playgroud)

堆栈闪电战https : //stackblitz.com/edit/angular-ohhi9e ? file = src%2Fapp%2Fapp.component.spec.ts

ggr*_*nig 6

操作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');
...
  }));
})
Run Code Online (Sandbox Code Playgroud)

来源:GitHub 上的 debounceTime

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