繁体   English   中英

Jest - 检查方法是否可以访问 object 中的数组

[英]Jest - To check if a method could access an array in the object

我有一个迭代 object 的方法,它嵌套了 arrays。我想知道是否有可能测试我的方法是否按预期迭代这些 arrays。

Object:

object = {
id: 's8003',
array_1: [
{
id: 'value1',
array_2: [
{
id: 'value2',
},
],
},
],
};
method_1(value: string): Result | null {
if (this.object) {
for (let i = 0; i < this.#object.array_1.length; i++) {
for (let j = 0; j < this.#object.abschnittsEingaben[i].array_2.length; j++) {
this.#result= this.#object.array_1[i].array_2[j];
if (this.#result) {
this.#result.id= this.#object.array_1[i].array_2[j].id;
if (this.#result.id === value) {
return Result;
}
}         
}       
}     
}
return null;   
}

**以上是我正在尝试的伪代码。 我想开个玩笑,它通过每个 if 案例的方法并检查数组是否存在。

目前我已经尝试检查整个 object,并且能够得到结果。 提前致谢**

如果你正在遍历一个数组,什么时候循环失败? 当您尝试访问undefined的变量时,它会发生。

所以在这里你期望this.#object.array_1[i].array_2[j]总是defined

test('should be defined', () => {
 expect(value).toBeDefined();
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM