简体   繁体   English

角度测试无法读取未定义的属性“列”

[英]Angular testing Cannot read property 'column' of undefined

I am trying to write a test case for the component but I am getting the error as Cannot read property 'column' of undefined. 我正在尝试为组件编写一个测试用例,但我收到错误,因为无法读取未定义的属性“列”。 I am using angular 6.I am trying to write a test case for the component but I am getting the error as Cannot read property 'column' of undefined. 我正在使用角度6.我正在尝试为组件编写测试用例,但我收到错误,因为无法读取未定义的属性“列”。 I am using angular 6. 我正在使用角6。

Here My code for the component 这是我的组件代码

import { Component, Inject } from '@angular/core';
import { ICellRendererAngularComp } from 'ag-grid-angular';
import { faEdit, faTrashAlt } from '@fortawesome/free-solid-svg-icons';
import { Router } from '@angular/router';
import { MatDialog } from '@angular/material/dialog';
import { ConfirmDialogComponent } from 'src/app/common/confirm-dialog/confirm-dialog.component';

@Component({
  selector: 'ap-grid-render',
  template: `
    <div *ngIf="params.column === 'status'">
      <mat-chip *ngIf="params.value === 'In progress'" color="primary" selected>
        {{ params.value }}
      </mat-chip>
      <mat-chip *ngIf="params.value === 'Approved'" color="accent" selected>
        {{ params.value }}
      </mat-chip>
      <mat-chip *ngIf="params.value === 'Rejected'" color="warn" selected>
        {{ params.value }}
      </mat-chip>
    </div>
    <div *ngIf="params.column === 'edit'">
      <button
        mat-icon-button
        color="accent"
        matTooltip="Edit request"
        matTooltipPosition="above"
        (click)="goToEdit()"
      >
        <fa-icon [icon]="faEdit"></fa-icon>
      </button>
      <button
        mat-icon-button
        color="accent"
        matTooltip="Delete"
        matTooltipPosition="above"
        (click)="deleteDomain()"
      >
        <fa-icon [icon]="faTrashAlt"></fa-icon>
      </button>
    </div>
  `
})
export class GridRendererComponent implements ICellRendererAngularComp {
  public params: any;
  // Icons
  faEdit = faEdit;
  faTrashAlt = faTrashAlt;

  constructor(private router: Router, public dialog: MatDialog) {}

  agInit(params: any): void {
    this.params = params;
  }

  goToEdit() {
    this.router.navigate(['./n-access-request']);
  }

  deleteDomain() {
    this.dialog.open(ConfirmDialogComponent, {
      panelClass: '_small-dialog',
      disableClose: true,
      position: { top: '50px' },
      data: {
        title: 'Delete Domain',
        description: `Are you sure do you want to delete domain Contract ID: ${
          this.params.data.contactId
        }?`
      }
    });
  }

  refresh(params: any) {
    this.params = params;
    return true;
  }
}

Here is the below code for spec which I tried 以下是我尝试的规范的下面代码

it('agInit', () => {
    let params = {
          "column" : "status"
           };
    gridRendererComponent.agInit(params);
    expect(gridRendererComponent.params).not.toBe(null);
  });

The error is caused as the value of params is undefined when trying to access from template. 由于在尝试从模板访问时未定义params的值,因此导致该错误。 To solve it, use safe navigation operator ? 要解决它,请使用安全的导航操作员? .

Instead of, 代替,

    params.column

Use, 采用,

    params?.column

You might also wanna consider changing params.value to params?.value or it will also throw an error. 您可能还想考虑将params.value更改为params?.value或者它也会引发错误。

Declare the type(class) of params instead of any 声明params的类型(类)而不是any

 public params: Params;


 export class Params {
    public column :any
    public value :any
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 测试无法读取未定义的属性“名称” - Angular testing Cannot read property 'name' of undefined Angular 测试:无法读取未定义的属性“图像” - Angular Testing: Cannot read property 'images' of undefined Angular 测试 - 类型错误:无法读取未定义的属性“7” - Angular Testing - TypeError: Cannot read property '7' of undefined 角度测试无法读取未定义的属性“数据” - Angular testing Cannot read property 'data' of undefined Angular 2单元测试“无法读取未定义的属性&#39;取消订阅” - Angular 2 unit testing “Cannot read property 'unsubscribe' of undefined” 在角度进行单元测试时,无法读取未定义错误的属性“导航” - Cannot read property 'navigate' of undefined error when unit testing in angular TypeError:无法读取未定义的属性&#39;id&#39;-Angular 8 Testing - TypeError: cannot read property 'id' of undefined - Angular 8 Testing TypeError:在单元测试Angular 6时无法读取未定义的属性“查询” - TypeError: Cannot read property 'query' of undefined while unit testing Angular 6 Angular 2单元测试 - 无法读取未定义的属性“root” - Angular 2 Unit Testing - Cannot read property 'root' of undefined 角度单元测试TypeError:无法读取未定义的属性“订阅” - Angular unit testing TypeError: Cannot read property 'subscribe' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM