简体   繁体   English

如何在测试之前等待异步ngOninit完成

[英]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.

相关问题 如何在我的 ngOnInit 方法上不使用异步来等待我的呼叫? - How can i wait for my call without using async on my ngOnInit method? 子组件在初始化之前要等待父组件ngOnInit异步操作 - Child Component to wait for Parent Component ngOnInit async operation before initializing 如何让Angular 5等待Injectable构造函数中使用的Promise在构造依赖项之前解析 - 或者ngOnInit? - How can I make Angular 5 wait for a Promise used inside an Injectable's constructor to resolve before constructing dependants - or ngOnInit? 在 ngAfterViewInit 中等待 NgOnInit 完成 - Wait for NgOnInit to complete inside ngAfterViewInit 如何在 Angular 动态组件内部等待 NgOnInit 完成? - How to wait for NgOnInit to complete inside Angular Dynamic Component? 如何在Angular 2+中执行多个http请求,但等待每个请求完成后再执行下一个? - How can I perform multiple http request in Angular 2+ but wait for each to complete before doing the next? 如何等待角度请求完成以进行自动化测试 - How to wait for angular requests to complete for automation testing 在ngOnInit Angular之前处理异步诺言 - Handle async promise before ngOnInit Angular 如何让该功能等待另一个功能完成? - How can I make that function wait for another function to complete? 如何在ngOnInit中使用await / async? - How to use await/async in ngOnInit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM