TypeError: studentsList.forEach is not a function. Jasmine Unit Testing (Angular 2)

TypeError: studentsList.forEach is not a function. Jasmine Unit Testing (Angular 2)

如何使用 karma 测试 jasmine 中的 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;
});
}
}

studentsList 中的数据是:-

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

    [{
        "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');
}

但我一直认为这个 forEach 不是一个函数。我应该采用其他方式而不是在测试中提供静态数据吗?

studentsList是一个对象,但是forEach只存在于数组中。如果您愿意,可以使用 for (var key in studentsList) 来迭代对象的键。