TypeError:sustomeList.Foreach不是一个功能.茉莉单元测试(角2)



如何使用业力在茉莉花中测试foreach循环?组件代码如下: -

getData(studentsList:any, day:any, game:any){
let strength:any;
let location:string;
   if(studentsList){
    studentsList.forEach( value => {
     strength=value.schedule[day][game].strength;
     location=value.schedule[day][game].location;
});
}
}

学生名单中存在的数据是: -

(编辑: - 数据已更新,看起来像这样)

    [{
        "activityName": "tournament1",
        "schedule": {
            "first": {
                "Baseball": {
                    "strength": 23,
                    "location": "abc"
                }
            },
            "second": {
                "Cricket": {
                    "strength": 20,
                    "location": "bcd"
                }
            },
            "third": {
                "Football": {
                    "strength": 19,
                    "location": "cde"
                }
            }
        }
    },
{
        "activityName": "tournament2",
        "schedule": {
            "first": {
                "Baseball": {
                    "strength": 23,
                    "location": "abc"
                }
            },
            "second": {
                "Cricket": {
                    "strength": 20,
                    "location": "bcd"
                }
            },
            "third": {
                "Football": {
                    "strength": 19,
                    "location": "cde"
                }
            }
        }
    },{
        "activityName": "tournament3",
        "schedule": {
            "first": {
                "Baseball": {
                    "strength": 23,
                    "location": "abc"
                }
            },
            "second": {
                "Cricket": {
                    "strength": 20,
                    "location": "bcd"
                }
            },
            "third": {
                "Football": {
                    "strength": 19,
                    "location": "cde"
                }
            }
        }
    }]

这是我尝试的测试: -

it('should check getData()', () => {
        fixture = TestBed.createComponent(GameComponent);
        const app = fixture.debugElement.componentInstance;
        const data={
    "activityName": "tournament",
    "schedule": {
        "first": {
            "Baseball": {
                "strength": 23,
                "location": "abc"
            }
        },
        "second": {
            "Cricket": {
                "strength": 20,
                "location": "bcd"
            }
        },
        "third": {
            "Football": {
                "strength": 19,
                "location": "cde"
            }
        }
    }
}
app.getData(data, 'first', 'Baseball');
fixture.detectChanges();
expect(app.location).toContain('abc');
expect(app.location).toContain('bcd');
expect(app.location).toContain('cde');
}

但是我继续获得此范围不是一个功能。我不应该在测试中提供静态数据?

studentsList是一个对象,但是 forEach仅在数组上存在。如果您想做的话,您可以使用for (var key in studentsList)迭代对象的键。

最新更新