我有以下角度分量:
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