我有一个正在尝试测试的功能。我在其中使用 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。
关于angular - 使用 mat paginator 方法 firstPage() 进行功能单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55188450/