【发布时间】:2020-08-08 23:17:09
【问题描述】:
如果你能帮助我理解一些事情,我将非常感激。 我有一个组件,它显示一些数据。在 ngOnInit() 中调用 http-service 方法从服务器获取数据。这是代码:
// isLogged - is get property
ngOnInit() {
if (this.isLogged) {
this.loadData();
};
}
// Can't call in ngOnInit because this method is being used in other functions
loadData() {
this.http.getData()
.subscribe((result) => this.dataToDisplay = result);
}
get isLogged() {
// this function checks for token in localStorage via custom token service.
}
我为 loadData() 编写测试
it('should load data', async(() => {
component.loadData();
fixture.whenStable().then( () => {
fixture.detectChanges();
expect(component.dataToDisplay).toEqual(mockData);
});
});
它按预期工作。但我的问题是:我应该如何测试 ngOnInit?这个方法所做的只是调用 loadData。但是如何检查 get 属性是否返回 true 方法正在调用?我很困惑((
【问题讨论】:
标签: angular unit-testing