angular - 单元测试 ngAfterViewInit

标签 angular unit-testing jestjs karma-jasmine angular-unit-test

Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value for 'dataSource': 'undefined'. Current value: '[object Object]'.

在我的 component.ts 中,我收到此错误。由于 ngafterviewinit 中有代码。

  ngAfterViewInit(): void {
    this.role = this.aut.getuser();
    if (this.role ===  undefined) {
      this.router.navigate(['/']);
    }

    this.ELEMENT_DATA_RED_MALE = this.resul.fetchresults('red', 'male');
    this.dataSourceRedMale = this.ELEMENT_DATA_RED_MALE;
    this.ELEMENT_DATA_RED_FEMALE = this.resul.fetchresults('red', 'female');
    this.dataSourceRedFemale = this.ELEMENT_DATA_RED_FEMALE;
  
    this.ELEMENT_DATA_YELLOW_MALE = this.resul.fetchresults('yellow', 'male');
    this.dataSourceYellowMale = this.ELEMENT_DATA_YELLOW_MALE;
    this.ELEMENT_DATA_YELLOW_FEMALE = this.resul.fetchresults('yellow', 'female');
    this.dataSourceYellowFemale = this.ELEMENT_DATA_YELLOW_FEMALE;
  
    this.ELEMENT_DATA_BLUE_MALE = this.resul.fetchresults('blue', 'male');
    this.dataSourceBlueMale = this.ELEMENT_DATA_BLUE_MALE;
    this.ELEMENT_DATA_BLUE_FEMALE = this.resul.fetchresults('blue', 'female');
    this.dataSourceBlueFemale = this.ELEMENT_DATA_BLUE_FEMALE;
  
    this.ELEMENT_DATA_GREEN_MALE = this.resul.fetchresults('green', 'male');
    this.dataSourceGreenMale = this.ELEMENT_DATA_GREEN_MALE;
    this.ELEMENT_DATA_GREEN_FEMALE = this.resul.fetchresults('green', 'female');
    this.dataSourceGreenFemale = this.ELEMENT_DATA_GREEN_FEMALE;
  }

请指导我如何测试 ngAfterviewInit,我是 Jasmine karma 测试的新手。

最佳答案

我也有类似的问题。看着lifecycle hooks documentation ,我将 ngAfterViewInit 更改为 ngAfterContentInit 并且它起作用了。

引用链接:ExpressionChangedAfterItHasBeenCheckedError Explained

关于angular - 单元测试 ngAfterViewInit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68362753/

相关文章:

html - 如何使图标在 html 表格行中可见?

unit-testing - 什么是单元测试数字音频生成的好技术

javascript - 在 Jest 中等待 Observer 完成

reactjs - Jest 无法加载时刻

angular - 发生未处理的异常 : Missing required <target> element

angular - 如何在 Angular 7 中设置超时 KeyUp

ios - Xcode 单元测试缺少某些项目类

c# - 使用 C# - .Net Core 进行单元测试 RabbitMQ 推送

jestjs - Apollo Boost MockedProvider 在查询中使用片段时返回空对象

django - 在 Heroku 上部署带有 Django Rest Framework 后端的 Angular 4 前端