测试用例失败并显示未定义属性的错误。
我将 roleID 传递给 getResults()。当我运行 ng test for ('it should create component').truthy 时,它会抛出一个错误 --> 未定义的角色 ID。
我也在 HTML 中使用过像 *ngIf="x?.roleID" 之类的,在那里我遇到了同样的错误。
*.spec.ts
文件示例:
// it('should create', () => {
// expect(component).toBeTruthy();
// });
// While running the ng Test for this Component, it is throwing error like ngOnit() Id of undefined property
如果this.inputArray
在您的组件中未定义,则该ngOnInit
方法将在尝试访问该roleID
属性时失败。在测试期间,您必须确保正确初始化您的输入,即inputArray
属性,这可以通过
a)将您的测试包装在充分设置输入的测试驱动程序组件中,例如通过使用如下模板:
<your-component [inputArray]="validArray"></your-component>
b)在准备测试时初始化您的组件属性:
beforeEach(() => {
fixture = TestBed.createComponent(YourComponent);
component = fixture.componentInstance;
// assign valid array to property here:
component.inputArray = [{roleID: 1, ...}];
// ... your ngOnInit will be run now:
fixture.detectChanges();
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句