使用垫分页器方法firstPage()进行功能的单元测试

时间:2019-03-15 18:06:02

标签: angular jasmine angular-material angular-test

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

"Cannot read property 'firstPage' of undefined"

我无法在单元测试中找到任何关于如何模拟分页器的信息,也无法在我的规格文件中提供任何信息来完成此测试。

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

1 个答案:

答案 0 :(得分:0)

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