在angular文档中,我看到了这两个函数,tick()
和flush()
.这两种方法似乎做了相似的事情.从angular文档中可以看出,对于tick:
模拟fakeAsync区域中计时器的异步时间流逝.
至于同花顺:
通过排空宏任务队列直到其为空,模拟fakeAsync区域中计时器的异步时间流逝.返回的值是经过的毫秒数.
谁能给我解释一下区别吗?
编辑(在 comments 中回答):
此外,在angular documentation中,tick()
使用时没有参数,该行上的注释甚至使用了短语"flush"
it('should display error when TwainService fails', fakeAsync(() => {
// tell spy to return an error observable
getQuoteSpy.and.returnValue(
throwError('TwainService test failure'));
fixture.detectChanges(); // onInit()
// sync spy errors immediately after init
tick(); // flush the component's setTimeout()
fixture.detectChanges(); // update errorMessage within setTimeout()
expect(errorMessage()).toMatch(/test failure/, 'should display error');
expect(quoteEl.textContent).toBe('...', 'should show placeholder');
}));