Angular 中测试用例的未定义属性错误

萨蒂什·斯里尼瓦桑

测试用例失败并显示未定义属性的错误。

我将 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Angular 2单元测试错误无法获取未定义或空引用的属性“ preventDefault”

Angular 4单元测试错误“ TypeError:无法读取未定义的属性'subscribe'”

错误TypeError:无法读取Angular 7拖放中未定义的属性'length'

Angular Material sidenav错误:无法读取未定义的属性

无法读取Angular 2中未定义的属性“错误”?

错误:无法读取未定义Angular 5的属性'cloneDeep'

Angular + Redux Jasmine测试在运行中随机抛出“无法读取未定义的属性'dispatch'”

Angular @ViewChild错误'无法读取未定义的属性nativeElement”

错误$在Angular Universal中未定义

从Angular 7更新为8后出现“无法读取未定义的属性'map'中的错误”

Angular 9.0.4:使用$ localize:无法读取未定义的属性'loadChildren'中的错误

错误TypeError:无法读取Angular 8中未定义的属性“值”

类型错误:无法读取Angular / Jasmine中未定义的属性“ next”

角组件测试用例出现错误:类型错误:无法读取未定义的属性“ contractno”

错误无法在Angular Material中设置未定义的属性'filterPredicate'

Angular Production构建错误“无法读取未定义的属性'toLowerCase'中的错误”

错误类型错误:无法读取 Angular 4 中未定义的属性“setupScene”

单元测试 Angular 4 出现错误“TypeError:无法读取未定义的属性“订阅”

类型错误:无法读取 Angular 应用程序中未定义的属性“长度”

Angular CLI 错误无法读取未定义的属性“写入”

Angular 6 中的“错误类型错误:无法读取未定义的属性‘名称’”

类型错误:在单元测试 Angular 6 时无法读取未定义的属性“查询”

如何修复 Angular 发布请求中的“无法读取未定义属性”错误

Angular 如何从配方错误中删除照片无法读取未定义的属性“拼接”

类型错误:无法读取 Angular 8 中未定义的属性“sendNotification”

类型错误:无法读取未定义的 Angular 的属性“提交”

Angular 无法读取未定义错误的属性订阅

错误类型错误:无法在 Angular 中读取未定义的属性(读取“removeVietnameseTones”)

错误类型错误:无法在 Angular 中读取未定义 afterViewInit 的属性