gpt4 book ai didi

angular - 从 async 和 fakeAsync 测试 intervalTimer

转载 作者:太空狗 更新时间:2023-10-29 19:27:37 24 4
gpt4 key购买 nike

Angular 测试指南部分关于 testing components with asynchronous services说:

Writing test functions with done, while more cumbersome than async and fakeAsync, is a viable and occasionally necessary technique. For example, you can't call async or fakeAsync when testing code that involves the intervalTimer, as is common when testing async Observable methods.

有谁知道 intervalTimer 是什么,或者为什么它不能通过 asyncasyncFake 进行测试?

最佳答案

intervalTimer 很可能是指 setInterval 或任何其他计时器功能,以及它不能与 asyncfakeAsync是因为async(据我理解)通常使用ComponentFixture上的whenStable函数,而 whenStable 实际上永远不会被调用,因为计时器 promise 永远不会完成。使用 fakeAsync 实际上会抛出一个错误,因为它会尝试同步执行测试,但当它完成时,它会检测到还有一个 promise 尚未解决,因此会抛出一个错误。

我是通过在 Angular 的 GitHub 存储库上发布问题得到的。这是问题的链接:https://github.com/angular/angular/issues/20711

关于 whenStable 的部分不在那个问题中,但从我自己的研究来看,通常使用 whenStableasync在一起。

关于angular - 从 async 和 fakeAsync 测试 intervalTimer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47562222/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com