【问题标题】:Angular/jasmine : TypeError: Cannot read property 'labels' of undefined AngularAngular/jasmine:TypeError:无法读取未定义 Angular 的属性“标签”
【发布时间】:2021-07-23 04:48:20
【问题描述】:

我为组件编写单位角度。当我运行测试时,终端显示错误。 TypeError:无法读取未定义的属性“标签” 运行测试用例时出现3个函数错误

private checkRating(): boolean {
 if ((Object.keys(this.ratingData.labels).length) > 0) {
   return true;
 } else {
   return false;
  }
}

ngAfterContentChecked(): void {
   this.handlerBallonBar();
}
   private handlerBallonBar() {
 return this.ratingOptions = {
   title: {
     display: false
   },
   legend: {
     position: 'right',
     display: false
   },
   scales: {
     xAxes: [
       {
         ticks: {
           beginAtZero: true,
         },
         display: false,
       }
     ],
     yAxes: [
       { display: this.checkRating() }
     ]
   },
   responsive: true,
   plugins: {
     datalabels: {
       labels: {
         value: {
           color: '#ffffff'
         }
       },
       font: {
         size: '12'
       }
     }
   }
 }
 }

这是一个定义测试用例

describe('BalloonComponent', () => {
  let component: BalloonComponent;
  let fixture: ComponentFixture<BalloonComponent>;

beforeEach(async(() => {
  TestBed.configureTestingModule({
    declarations: [BalloonComponent]
  }).overrideTemplate(BalloonComponent, '');
 }));

beforeEach(() => {
  fixture = TestBed.createComponent(BalloonComponent);
  component = fixture.componentInstance;
  fixture.detectChanges();
 });
it('should create', () => {
  expect(component).toBeTruthy()
 });

这是错误 enter image description here

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    你能更新如下代码吗

    private checkRating(): boolean {
            if (this.ratingData && this.ratingData.labels && (Object.keys(this.ratingData.labels).length) > 0) {
                return true;
            } else {
                return false;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-17
      • 2017-11-09
      • 1970-01-01
      • 2020-08-10
      • 2020-11-07
      • 1970-01-01
      • 2016-01-12
      • 2015-09-08
      相关资源
      最近更新 更多