我如何在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();
});
Component:
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;
资源:
您需要初始化值:
@Input() jsonData: PropertySitusAddressContainer = {
existingPropertySitusAddress = {}
}
这样,使用.
运算符访问的所有字段都需要初始化。
在您的html中,对于value = "jsonData?.existingPropertySitusAddress?.primaryPropertyMailingAddressId"
,使用type safety
将是一个不错的选择。