【发布时间】:2018-04-01 07:24:21
【问题描述】:
我正在尝试找出 fakeAsync 的 tick() 方法与 done() 的区别,正如一些 answers 在堆栈溢出时所建议的那样。
使用tick() 我们可以模拟超时,但是我们可以使用done() 来实现同样的效果吗?
为什么 Angular 认为 more viable method 而不是使用 async 或 fakeAsync?
举个例子。
这个方法对我有用...
it("Should display names",(done:any) => {
component.names = [
{
"firstname": "abc",
"lastname": "max"
},
{
"firstname": "def",
"lastname": "max"
},
];
done();
fixture.detectChanges();
let wrapBox = fixture.debugElement.queryAll(By.css('.wrapBox'));
console.log(wrapBox);
});
但以下方法返回 '6 timer(s) still in queue' 错误...
it("Should display names",fakeAsync(() => {
component.names = [
{
"firstname": "abc",
"lastname": "max"
},
{
"firstname": "def",
"lastname": "max"
},
];
tick();
fixture.detectChanges();
let wrapBox = fixture.debugElement.queryAll(By.css('.wrapBox'));
console.log(wrapBox);
}));
注意:
数组
names的数据是异步的,因为它是使用“get”操作从后端检索的。但在这里我是在嘲笑数据。数组中的数据被遍历并传递给另一个子组件,在视图中显示它。
【问题讨论】:
-
这篇文章很好理解blog.nrwl.io/…
-
@AngularInDepth.com 感谢您的链接!值得一读!
标签: angular unit-testing typescript karma-jasmine