繁体   English   中英

带有 jasmine karma 的 Angular 单元测试错误(finalSprintsList.map 不是函数)

[英]Angular unit test error ( finalSprintsList.map is not a function ) with jasmine karma

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

setResponseForButton() {
 let sortedList = this.teamboardService.sortSprintsBasedOnEndDate();
 this.populateSprintDropDown(sortedList);
}

//finalSprintList that is returned by service is = [{id: "3712", name: "DCT 3.128.1 Sp1 (11Dec-24Dec)", 
status: "ACTIVE", sprintLink: null, startDate: "2019-12-11"}]


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

但是当我运行我的测试时,我收到了这个错误:

类型错误:finalSprintsList.map 不是函数。 我无法理解这是什么意思以及如何解决它。

您应该模拟teamboardService.sortSprintsBasedOnEndDate方法。

请注意,方法component.populateSprintDropDown不需要间谍,因为您可以检查由此方法更新的值component.sprintsList

尝试按如下方式重写您的测试:

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

    // given
    const sortedList = [{
                          id: "3712",
                          name: "DCT 3.128.1 Sp1 (11Dec-24Dec)",
                          status: "ACTIVE",
                          sprintLink: null,
                          startDate: "2019-12-11"
                       }];
    spyOn(component.teamboardService, 'sortSprintsBasedOnEndDate').and.returnValue(sortedList);

    // when
    component.setResponseForButton(); 

    // then
    const expected = [{item_id: "3712", item_text: "DCT 3.128.1 Sp1 (11Dec-24Dec)"}];
    expect(component.sprintsList).toEqual(expected );
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM