我有一个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;
})
};
答案 0 :(得分:4)
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
}
);
}));