我有一个基于服务的功能来获取其时间并进行汇总。首先需要从mongoDb数据库中获取服务,因此我编写了一个函数async
从服务中获取时间的功能
const getTotalVisitsTime = async (services) => {
let totalVisitTime = 0
const foundServices = await require('../visitType').find({ _id: { $in: services } })
if (foundServices) {
return new Promise((resolve, reject) => {
if (foundServices.length === 0) {
reject('No services for given id(s) found in database')
} else {
for (var i = 0; i < foundServices.length; i++) {
totalVisitTime += parseInt(foundServices[i].time)
}
resolve(totalVisitTime)
}
})
} else {
throw Error('Problem with querying services ocurred. Check id(s)')
}
}
用法:
module.exports.getAvailableVisitTimesByDate = async (hairdresserId, date, services, salonData) => {
return getAvailableVisitTimesArray(await getVisitsForGivenDay(hairdresserId, date),
salonData,
await getTotalVisitsTime(services).catch(err => { console.log(err) }))
}
虽然通过Postman进行测试时,它会获取所有需要的数据,但是我想使用chakram
进行测试。不幸的是,在测试用例中,它没有获取时间,这导致测试中的数组为空。
测试用例:
it('should return array of available visit times for certain hairdresser, 1 hour-long visit and certain day (10-03-2019)', () => {
return chakram.get(url, requestBody, { headers: { 'content-type': 'application/json' } })
.then(response => {
expect(response).to.have.status(201)
console.log(response.body)
//expect(response.body['freeVisitTimes']).to.eql(expectedArrayOfTimes)
})
})
[在测试时记录为getTotalVisitsTime
为undefined
,而通过邮递员进行请求时会得到描述时间的整数。我的代码有什么问题?
const foundServices =等待require('../ visitType')。find({_id:{$ in:services}})。exec();不要忘记.exec();
[在chakram
模块中转一会儿使我相信,它只是request
模块的包装,支持Q
(或Promise)。