Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 在角度测试中,tick()和flush()之间有什么区别?_Angular_Typescript_Testing_Ionic Framework_Jasmine - Fatal编程技术网

Angular 在角度测试中,tick()和flush()之间有什么区别?

Angular 在角度测试中,tick()和flush()之间有什么区别?,angular,typescript,testing,ionic-framework,jasmine,Angular,Typescript,Testing,Ionic Framework,Jasmine,在angular文档中,我看到了这两个函数,和。这两个似乎做了相似的事情。从angular文档中可以看出,对于tick: 模拟fakeAsync区域中计时器的异步时间流逝 至于同花顺: 通过排空宏任务队列直到其为空,模拟fakeAsync区域中计时器的异步时间流逝。返回的值是经过的毫秒数 有人能给我解释一下区别吗 编辑(在评论中回答): 此外,在tick()中,使用时不带参数,该行上的注释甚至使用短语“flush” it('TwainService失败时应显示错误',fakeAsync(()=>

在angular文档中,我看到了这两个函数,和。这两个似乎做了相似的事情。从angular文档中可以看出,对于tick:

模拟fakeAsync区域中计时器的异步时间流逝

至于同花顺:

通过排空宏任务队列直到其为空,模拟fakeAsync区域中计时器的异步时间流逝。返回的值是经过的毫秒数

有人能给我解释一下区别吗

编辑(在评论中回答):

此外,在
tick()中,使用
时不带参数,该行上的注释甚至使用短语“flush”

it('TwainService失败时应显示错误',fakeAsync(()=>{
//告诉spy返回可观察到的错误
getQuoteSpy.and.returnValue(
投掷者(“TWAINNERVICE测试失败”);
fixture.detectChanges();//onInit()
//在初始化后立即同步间谍错误
tick();//刷新组件的setTimeout()
fixture.detectChanges();//setTimeout()内更新错误消息
expect(errorMessage()).toMatch(/test failure/,'should display error');
expect(quoteEl.textContent).toBe(“…”,“应显示占位符”);
}));

相对于先前启动的异步操作,它们执行不同的操作。例如调用
setTimeout(…)
启动异步操作

  • tick()
    向前移动时间
  • flush()
    将时间移到最后
这些函数的单元测试可以更好地说明这一点

打上钩 此单元测试显示,在所有10个计时器都完成之前,滴答声用于将时间逐步向前移动。Tick被多次调用

脸红 这个单元测试显示所有异步任务在返回时都应该完成,并且返回的值告诉您完成这些任务需要多长时间


这是有道理的,但从理论上看,使用tick()而不使用任何参数似乎可以有效地进行刷新。@JustinKavalan是的,我也读过。因此,基本上没有参数,这意味着
tick()
会一直向前移动。因此,它与
flush()
相同。区别在于,
tick()
不会返回经过的时间。@JustinKavalan,如果这回答了您的问题。请接受它作为答案。我只是想补充一点,
tick()
flush()
是不相等的。我有一个测试,在那里我监视
路由器。在点击触发的订阅中导航
,使用
tick()
在队列中留下一个定期计时器,而
flush()
没有。(有趣的是,
flush()
在这个测试中总是返回500毫秒。)我认为没有参数的
tick()
只运行即时计时器,比如
setTimeout(()=>doSomething(),0)
flush()
基本上是
while(testZone.hasPendingTasks){tick(1);}

      it('should clear periodic timers', fakeAsync(() => {
           let cycles = 0;
           const id = setInterval(() => { cycles++; }, 10);

           tick(10);
           expect(cycles).toEqual(1);

           discardPeriodicTasks();

           // Tick once to clear out the timer which already started.
           tick(10);
           expect(cycles).toEqual(2);

           tick(10);
           // Nothing should change
           expect(cycles).toEqual(2);
         }));
      it('should flush multiple tasks', fakeAsync(() => {
           let ran = false;
           let ran2 = false;
           setTimeout(() => { ran = true; }, 10);
           setTimeout(() => { ran2 = true; }, 30);

           let elapsed = flush();

           expect(ran).toEqual(true);
           expect(ran2).toEqual(true);
           expect(elapsed).toEqual(30);
         }));