Wie testet man die forEach-Schleife in Jasmin mit Karma ?? Der Komponentencode ist wie folgt: -TypeError: studentsList.forEach ist keine Funktion. Jasmin Unit Testing (Angular 2)
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;
});
}
}
die Daten in studentsList ist: -
(Edit: - die Daten aktualisiert werden es so aussieht)
[{
"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"
}
}
}
}]
Dies ist der Test, den ich ausprobiert habe: -
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');
}
Aber ich bekomme das immer weiter, denn es ist keine Funktion. Anstatt statische Daten im Test zu geben, sollte ich einen anderen Weg gehen?
'forEach' erfordert ein Array –