angular - 使用 mat 分页器方法 firstPage() 对函数进行单元测试
问题描述
我有一个我正在尝试测试的功能。我在其中使用 matPaginator.firstPage() 方法。但是,我为此功能编写的任何测试都可能出现错误
"Cannot read property 'firstPage' of undefined"
我找不到任何关于如何在单元测试中模拟分页器或在我的规范文件中提供它以允许该测试完成的任何信息。
任何帮助/建议/提示将不胜感激。
解决方案
it('area of business selected should be in active Filter Chip Object', () => {
component.selectedBusinessArea = 'Fake Business';
component.activeFilterChips = [];
component.activeFilterObj = {};
fixture.detectChanges();
fixture.whenStable().then(() => {
spyOn(component.paginator, 'firstPage').and.returnValue('');
component.startFilter();
fixture.detectChanges();
expect(component.activeFilterChips).toBe([{
option: "businessArea",
value: "Fake Business"
}]);
})
});
为分页器的 firstPage() 创建一个 spyOn 并将其包装在 whenStable() 中,我的测试通过了。不确定这是否是正确的处理方式。另外,我没有尝试测试 firstPage(),所以我给了 returnValue ''。
推荐阅读
- python - Python 如何阻止自己的递归?
- vba - 访问 VBA 列出文件夹及其所有子文件夹中的所有文件
- sql-server - 创建时出现错误“找不到任何名为 [IX_MyIndex] 的索引”
- json - 如何通过 API 使用映射函数和 JSON 对象获取
- python-3.x - str 对 csv 格式的响应
- python - 如何在“pip install”命令中指定依赖项的版本?
- reactjs - 为什么不允许在 React 中返回多个元素?
- javascript - Angular Js/Javascript 执行一个函数,直到数组的所有内容都被使用
- algorithm - 硬编码值来自散列/压缩/编码算法
- vb.net - 进度条总和值不正确