Angular 单元测试错误(finalSprintsList.map 不是函数)与 jasmine karma
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();
});
但是当我 运行 我的测试我得到这个错误:
TypeError: 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 );
});
我正在尝试为我的组件方法编写单元测试,即 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();
});
但是当我 运行 我的测试我得到这个错误:
TypeError: 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 );
});