[英]How can I wait for async ngOninit to complete before testing
I have an ngOnInit
method that does async work. 我有一个执行异步工作的ngOnInit
方法。
How can I execute my expect
statements once the async work is complete ? 异步工作完成后,如何执行expect
语句?
it('test things once all the promises declared in ngOninit are resolved', () => {
comp.ngOnInit();
// I want to test that comp.property is like expected
});
My component methods look like this: 我的组件方法如下所示:
OnInit() {
this.asyncMethod();
};
asyncMethod() {
this.methodThatReturnsPromise().then(() => {
this.property = this.otherPropertyNowResolved;
})
};
fixture = TestBed.createComponent(MyComponent);
it('test things once all the promises declared in ngOninit are resolved', async(() => {
fixture.detectChanges();
fixture.whenStable().then(
() => {
// This should run once ngOnInit is completed
}
);
}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.