【发布时间】:2019-09-30 02:03:31
【问题描述】:
我正在努力为以下方法编写 JEST 测试用例
getStudentList (studentList:}[]) {
if (studentList.length < 1) {
Promise.resolve()
}
let promises = []
for (const student of StudentList) {
if (!student.name) {
Promise.resolve()
}
var url = `${API_URL}/${student.name}/`
promises.push(Axios.get(url}))
}
return Axios.all(promises)
.then(Axios.spread((...args) => {
// customise the response here
return args
.map(response => response.data)
.map(data => {
//do something with data
return data
})
}))
它使用 axios.all 和 axios.spread 来取回数据。我已经为 Axios.get 编写了简单的测试用例。但是如何为此编写测试用例?这个方法在一个vue项目中的一个服务类中
【问题讨论】:
-
请注意,您的实现存在很多问题,例如你的参数的默认值,
Promise.resolve()什么都不做,StudentList没有定义,Axios.spread有点没用,因为你使用的是 rest 运算符 -
你能详细说明一下其余操作符吗?
-
如何改进promise.resolve?如果学生列表为空,我只是不想做任何 axios 调用..
-
在for循环中,你应该使用
continue;或const students = studentList.filter(student => student.name) -
您能否发布一个单独的问题来改进
Axios.spread的代码?