Jasmine 单元测试:如何访问由 @input() 设置的 属性 ?使用 component.propertyname returns 未定义访问它
Jasmine unit testing: How to access property set by @input() ? Accessing it using component.propertyname returns undefined
ts 文件:
@input() selectedDep: WebMessagingDeployments[]
规格文件:
beforeEach(() => {
fixture = TestBed.createComponent(DeploymentAssignmentFormComponent);
component = fixture.componentInstance;
console.log(component.selectedDep) // returns undefined
component.selectedDep.push(mockDepData); //this line throws error. TypeError: Cannot read property '0' of undefined
}));
看起来您还没有在组件和测试文件中初始化 selectedDep
。
几个选项是:
在声明时初始化
@Input() selectedDep: WebMessagingDeployments[] = [];
在测试中初始化
fixture = TestBed.createComponent(DeploymentAssignmentFormComponent);
分量 = fixture.componentInstance;
component.selectedDep = [/* 这里的值 */];
fixture.detectChanges();
ts 文件:
@input() selectedDep: WebMessagingDeployments[]
规格文件:
beforeEach(() => {
fixture = TestBed.createComponent(DeploymentAssignmentFormComponent);
component = fixture.componentInstance;
console.log(component.selectedDep) // returns undefined
component.selectedDep.push(mockDepData); //this line throws error. TypeError: Cannot read property '0' of undefined
}));
看起来您还没有在组件和测试文件中初始化 selectedDep
。
几个选项是:
在声明时初始化
@Input() selectedDep: WebMessagingDeployments[] = [];
在测试中初始化
fixture = TestBed.createComponent(DeploymentAssignmentFormComponent); 分量 = fixture.componentInstance; component.selectedDep = [/* 这里的值 */]; fixture.detectChanges();