我有以下角度分量:
export class AppComponent implements OnInit {
data: Currency[];
ngOnInit() {
d3.csv('../../assets/data.csv').then((data: any) => {
this.data = data.map(item => new Currency(item.date, item.price));
});
}
}
我想为这个组件编写一个单元测试,这就是我目前所拥有的:
it('should ', async(() => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
fixture.whenStable().then(() => {
const app = fixture.debugElement.componentInstance;
expect(app.data.length).toBe(10);
});
}));
我相信fixture.whenStable 会等到所有承诺都得到解决(例如ngOnInit 中的承诺)。有人可以指导我编写一个正确的单元测试,等待承诺得到解决并且 this.data 具有价值吗?
到目前为止,这似乎是答案:Testing promise in Angular 2 ngOnInit