angular - 编写单元测试用例时无法将 undefined 或 null 转换为对象
问题描述
当我尝试为该函数运行单元测试时,我收到错误“无法将未定义或 null 转换为对象”
请指导我哪里出错了。
这是 ts 文件中的函数-
filterTargetWeightingFactor(data: any) {
const filterTargetWeight: any = [];
Object.keys(data).forEach(function(key) {
filterTargetWeight.push(key);
});
return filterTargetWeight.sort();
}
以下是该功能的规范文件中的代码 -
it('should return sorted data', () => {
const unsortedData = {
'b':'abc',
'c':'vgf',
'a':'sdf'
}
let sortedData = [ ];
Object.keys({'key': 'value'})
let finalArr = component.filterTargetWeightingFactor(unsortedData);
expect(finalArr).toBe(['a','b','c']);
});
解决方案
推荐阅读
- php - Xdebug 断点在 Sublime Text 3 上不起作用
- reactjs - Using React, how can sibling components best share a state? Should there even be a way to traverse DOM tree...?
- javascript - Material UI:如何检查哪个选项卡处于活动状态?
- vba - 连续的参考指示符用“-”分隔符组合成字符串,第一次和最后一次看到(VBA)
- ruby-on-rails - with_lock 会锁定块内的所有内容吗?
- javascript - 使用 puppeteer 单击元素列表
- laravel-livewire - 如何在多个输入 Laravel Livewire 中更新和显示正确的值
- typescript - 打字稿,将界面键转换为可能的字符串值的枚举
- rest - HERE.com 目的地天气 API 使用新创建的 API 密钥返回“禁止”
- javascript - Ionic React Android nativ 后退按钮不会改变视图