0

如何在 Angular Karma Jasmine 单元测试中声明和使用接口?以下给出错误,它说明接口的第一个属性未定义;试图让组件运行

无法读取未定义的“primaryPropertyMailingAddressId”的属性

业力/茉莉花:

 beforeEach(async(() => {

    fixture = TestBed.createComponent(PropertySitusFinalizeComponent);
    component = fixture.componentInstance;

    component.jsonData = {};  // removing or keeping this line does not change the error message

    fixture.detectChanges();

  }));

  it('should create', () => {
    expect(component).toBeTruthy();
  });

零件:

export class PropertySitusFinalizeComponent implements OnInit {

  @Input() jsonData: PropertySitusAddressContainer;

界面:

export interface PropertySitusAddressContainer {
  queueItemId?: number;
  existingPropertySitusAddress?: PropertySitusAddress;  

export class PropertySitusAddress {

    primaryPropertyMailingAddressId?:number = null;
    propertyId?: number = null;
    propertySitusAddressId?: number = null;
    addressFormatId?: number = null; 
    apn?: string = null;

资源:

如何在打字稿中对模型接口进行单元测试?

4

1 回答 1

0

您需要初始化这些值:

@Input() jsonData: PropertySitusAddressContainer = { existingPropertySitusAddress = {} }

这样,所有使用.运算符访问的字段都需要初始化。

在您的 html 中, usevalue = "jsonData?.existingPropertySitusAddress?.primaryPropertyMailingAddressId"将是type safety.

于 2020-02-10T06:27:19.780 回答