填寫這份《一分鐘調查》,幫我們(開發組)做得更好!去填寫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);
     }));
});