【发布时间】:2021-08-15 22:24:29
【问题描述】:
所以我的 axios 调用是一个已经分离的模块,并通过模拟 axios 本身进行了测试。但是,当我将此功能导入控制器时,测试控制器本身的最佳方法是什么?我知道它需要被模拟,但我不确定最有效的模拟方式,因此我可以测试 req 和 res(它们也被模拟)。
axios 调用是getAddressWithPostcode
控制器:
const findAddress = async (req: Request<Params>, res: Response, next: NextFunction) => {
const { params } = req
await getAddressWithPostcode(params)
.then((data) => {
if (!isUndefined(data.status)) {
return res.status(data.status).send(data.data.Message)
}
return res.send(data)
})
.catch(err => {
next(err)
})
}
Axios 调用:
const getAddressWithPostcode = async (params: Params) => {
const { postCode, number } = params
const addressUrl = `${process.env.URL}/${postCode}${number
? `/${number}?api-key=${process.env.API_KEY}`
: `?api-key=${process.env.API_KEY}`}`
try {
const { data } = await axios.get(addressUrl)
return data
} catch (e) {
const { response } = e
return response
}
}
【问题讨论】:
标签: javascript node.js typescript express jestjs