2017-09-26 2 views
1

Comment tester la boucle forEach dans le jasmin en utilisant le karma ?? Le code composant est le suivant: -TypeError: studentsList.forEach n'est pas une fonction. Jasmine 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; 
}); 
} 
} 

les données présentes dans studentsList est: -

(Edit: - l'on dirait que les données sont mis à jour ce)

[{ 
     "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" 
       } 
      } 
     } 
    }] 

C'est le test que j'ai essayé: -

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

Mais je continue à le faire carChaque fonction n'est pas une fonction. Au lieu de donner des données statiques dans le test dois-je aller d'une autre manière?

+1

'forEach' nécessite un tableau –

Répondre

1

studentsList est un objet, mais forEach n'existe que dans les tableaux. Vous pouvez utiliser for (var key in studentsList) pour itérer les clés de l'objet si c'est ce que vous voulez faire.

+0

Merci mon problème a été résolu en passant' data = [{ "activityName": "tournoi", "calendrier": { "first": { "Baseball": { "force": 23, "location": "abc" } }, "seconde": { "Cricket": { "force": 20, "location": "DCB" } } , "troisième": { "Football": { "force": 19, "location": "cde" } } } }] 'comme tableau – Jia