茉莉花因果,出现角单元测试错误(finalSprintsList.map不是函数)

问题描述 投票:0回答:1

我正在尝试为我的组件方法编写单元测试,即populateSprintDropDown。这是我的组件代码

setResponseForButton() {
 this.populateSprintDropDown(sortedList);
}

populateSprintDropDown(finalSprintList: Array<any>) {
  this.sprintsList = (<Array<Sprint>>finalSprintList).map(sprint => ({ item_id: sprint.id, item_text: sprint.name }));
} 

测试:

  it('populate sprint in sprint dropdown upon button click', () => {
    let list = [{item_id: "3712", item_text: "DCT 3.128.1 Sp1 (11Dec-24Dec)"}]
    component.sprintsList  = list;
    const spy = spyOn(component, 'populateSprintDropDown').and.callThrough();
    component.setResponseForButton(); 
    expect(spy).toBeDefined();
    expect(spy).toHaveBeenCalled();
  });

但是当我运行测试时,出现此错误:

TypeError:finalSprintsList.map不是函数。我不明白这是什么意思,以及如何解决。

angular unit-testing jasmine angular6 karma-jasmine
1个回答
0
投票

该问题必须与组件数据的初始化有关。您初始化component.sprintsList,应该在方法component.populateSprintDropDown中对其进行更改。我想,您想在调用component.sprintsList之前先初始化component.setResponseForButton

此外,在您的情况下也无需创建spy,只需断言component.sprintsList已更改就暗示已成功调用component.populateSprintDropDown

尝试如下重写测试:

it('dropdown upon button click should update sprintsList', () => {

    // given
    component.sortedList = [{ id: "3712", name: "DCT 3.128.1 Sp1 (11Dec-24Dec)" }];

    // when
    component.setResponseForButton(); 

    // then
    const expected = [{item_id: "3712", item_text: "DCT 3.128.1 Sp1 (11Dec-24Dec)"}];
    expect(component.sprintsList).toEqual(expected );
});
© www.soinside.com 2019 - 2024. All rights reserved.