首页 > 解决方案 > 使用 mat 分页器方法 firstPage() 对函数进行单元测试

问题描述

我有一个我正在尝试测试的功能。我在其中使用 matPaginator.firstPage() 方法。但是,我为此功能编写的任何测试都可能出现错误

"Cannot read property 'firstPage' of undefined"

我找不到任何关于如何在单元测试中模拟分页器或在我的规范文件中提供它以允许该测试完成的任何信息。

任何帮助/建议/提示将不胜感激。

标签: angularjasmineangular-materialangular-test

解决方案


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 ''。


推荐阅读