2017-09-26 3 views
1

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?

+1

'forEach' erfordert ein Array –

Antwort

1

studentsList ist ein Objekt, aber forEach existiert nur auf Arrays. Sie können for (var key in studentsList) verwenden, um die Schlüssel des Objekts zu iterieren, wenn Sie das möchten.

+0

Dank mein Problem, indem' data = [{ "Activity" gelöst wurde: "Turnier", "Zeitplan": { "first": { "Baseball": { "Stärke": 23, "location": "abc" } }, "zweite": { "Cricket": { "Stärke": 20, "location": "bcd" } } , "dritte": { "Fußball": { "Stärke": 19, "Ort": "cde" } } } }] 'als Array – Jia

Verwandte Themen