首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角度材料测试MatPaginator

角度材料测试MatPaginator
EN

Stack Overflow用户
提问于 2018-03-30 15:39:10
回答 2查看 3.4K关注 0票数 4

当我尝试测试一个组件的方法,该方法从角度材料设置MatPaginator的值时,我得到了这个错误:

代码语言:javascript
复制
TypeError: Cannot set property 'pageIndex' of undefined

下面是我的代码:

组件:

代码语言:javascript
复制
resetVacancySearch = () => {
    this.isVacanciesSearchMode = false;
    this.searchVacanciesValue = '';
    this.paginatorVacancies.pageIndex = 0; // reset to the first page
    this.paginatorVacancies.pageSize = 10;
    this.getVacanciesList(0, 10, 'name', 'ASC');
}

测试文件:

代码语言:javascript
复制
it('should call sourcingService.getVacanciesList and reset the search value from the input field', () => {
        spyOn(sourcingService, 'getVacanciesList').and.returnValue(Observable.of());
        component.resetVacancySearch();
        expect(sourcingService.getVacanciesList).toHaveBeenCalledWith(0, 10, 'name', 'ASC');
        expect(component.searchVacanciesValue).toEqual('');
 });

应用程序运行良好,但我无法将MatPaginator的实例放入测试文件。有人能建议我如何在测试中模拟MatPaginator吗?谢谢!

EN

回答 2

Stack Overflow用户

发布于 2018-11-02 16:39:38

只需添加

代码语言:javascript
复制
MatPaginatorModule,
BrowserAnimationsModule

在import[]数组中。

票数 1
EN

Stack Overflow用户

发布于 2020-07-01 17:06:16

我能够在测试类中访问组件中定义的分页器。

在component.ts中,我添加了下面这行代码来访问分页器

@ViewChild(MatPaginator, { static: false }) paginator: MatPaginator;

component.html

代码语言:javascript
复制
<div>
  <mat-paginator [pageSizeOptions]="pageSizeOptions" (page)="onPageChange($event)">
  </mat-paginator>
</div>

在component.spec.ts中,

代码语言:javascript
复制
beforeEach(async(() => {TestBed.configureTestingModule({
  declarations: [DiscoverAddTargetDialogComponent],
  imports: [NoopAnimationsModule,MatTableModule, MatSortModule, MatPaginatorModule],
  providers: []
})
  .compileComponents();}));

我按如下方式访问了分页器:

console.log("length :" + component.paginator.length);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49570401

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档