gpt4 book ai didi

angular - 在测试之前如何等待 async ngOninit 完成

转载 作者:行者123 更新时间:2023-12-02 10:44:11 31 4
gpt4 key购买 nike

我有一个执行异步工作的 ngOnInit 方法。

异步工作完成后,如何执行我的 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

});

我的组件方法如下所示:

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

}
);
}));

关于angular - 在测试之前如何等待 async ngOninit 完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44242892/

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