【发布时间】:2022-07-22 22:20:42
【问题描述】:
我试图弄清楚我的测试失败的原因。当页面加载时,从构造函数中的 localstorage 中获取一个值,并在调用方法 sortItems 之后
constructor(private loadingCtrl: LoadingController) {
this.details = JSON.parse(localStorage.getItem('details'));
this.sortItems('id')
}
sortItems(value) {
if (value === 'id') {
this.details.items = this.details.items.sort((a, b) =>
a.id > b.id ? 1 : b.id > a.id ? -1 : 0
);
}
if (value === 'name') {
this.details.items = this.details.items.sort((a, b) =>
a.name > b.name ? 1 : b.name > a.name ? -1 : 0
);
}
}
我的茉莉花测试用例如下
it('should create', () => {
component.details = order;
expect(component).toBeTruthy();
});
我在文件中有模拟数据的地方
const order = {
first_name: 'Michele T',
id: 41861,
items: [{
id: 135878,
name: 'Arden',
status: 'active'
}],
};
const mockLocationArray = [order];
export { order, mockLocationArray };
当我运行ng test 时,我收到错误Cannot read properties of null (reading 'items'),但项目已在测试用例中定义
【问题讨论】: