angular - Jasmine 单元测试:如何访问由 @input() 设置的属性?使用 component.propertyname 访问它会返回 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[] = [];
在测试中初始化
夹具 = TestBed.createComponent(DeploymentAssignmentFormComponent); 组件=fixture.componentInstance;component.selectedDep = [/* 这里的值 */]; 夹具.detectChanges();
推荐阅读
- symfony4 - Symfony 4,登录(或注销)后如何将用户重定向到上一页?
- sql - 如何合并我的数据以减少 MariaDB 中不同值的数量?
- java - 使用wireshark,是否可以捕获sqlprepared statement的参数?
- javascript - 如何过滤多维对象数组
- java - 更改片段更改的主题
- ubuntu - Nuxt.js 项目构建未在服务器上生成
- java - 如何限制下拉列表中的值?
- azure-spatial-anchors - Anchor 有时会随机跳开
- ios - 为 pjsip 构建时,在 libssl.a 中找不到架构 x86_64 的符号
- android - 具有自定义 GATT 服务的网络蓝牙?