填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写Home

fakeAsync

包装一个函数,以便在 fakeAsync Zone 中执行:

Wraps a function to be executed in the fakeAsync zone:

查看"说明"...

      
      fakeAsync(fn: Function): (...args: any[]) => any
    
参数
fn Function
返回值

要包装为在 fakeAsync Zone 中执行的函数

(...args: any[]) => any: The function wrapped to be executed in the fakeAsync zone

说明

  • 通过调用 flushMicrotasks() 手动执行微任务,

    microtasks are manually executed by calling flushMicrotasks(),

  • 计时器是同步的,用 tick() 模拟异步时间的流逝。

    timers are synchronous, tick() simulates the asynchronous passage of time.

如果函数末尾有任何待处理的计时器,则将引发异常。

If there are any pending timers at the end of the function, an exception will be thrown.

可用于包装 inject() 调用。

Can be used to wrap inject() calls.

Further information available in the Usage Notes...

使用说明

例子

Example

      
      describe('this test', () => {
  it('looks async but is synchronous', <any>fakeAsync((): void => {
       let flag = false;
       setTimeout(() => {
         flag = true;
       }, 100);
       expect(flag).toBe(false);
       tick(50);
       expect(flag).toBe(false);
       tick(50);
       expect(flag).toBe(true);
     }));
});