node.js - Nodejs test giving error when part of the test is modularized into a different function
问题描述
I have a Nodejs test which has below part
const response1 = await request({
method: HttpMethods.POST,
path: `/v1/incidents/${emptyIncidentId}/assessments`,
body: requestBody,
auth: authentication
})
And its giving the correct response. Now, I am trying to put the above into a separate async function because this gets repeated in many tests. So my function looks like below:
const response = doRequest(requestBody, emptyIncidentId, authentication)
And the function definition is :
async function doRequest (requestBody, incidentId, authentication) {
return request({
method: HttpMethods.POST,
path: `/v1/incidents/${incidentId}/assessments`,
body: requestBody,
auth: authentication
})
But this doRequest() call always responds with an empty response body. Could anyone tell me what I am doing wrong here. I checked the passed parameters, they are being passed correctly.
解决方案
当您调用该函数时,请尝试使用await
关键字。因为request
是异步的
const response = await doRequest(requestBody, emptyIncidentId, authentication);
推荐阅读
- excel - 在 Excel 中修改巨大的(350 万行)
- php - os.chdir OSError(13, 'Permission denied') for amps
- performance-testing - 是否可以使用 VSTest 任务在 VSTS Release Pipeline 中运行 .webtest 文件?
- python - 试图将旧的帕斯卡脚本翻译成 python:帕斯卡“记录”的问题
- python - Tkinter:为用户选择的点设置颜色
- python - Selenium Python:find_element_by_css_selector 中的无效选择器
- javascript - 在 JavaScript 中的自调用函数中初始化插件
- ios - 如何在点击类似 Tinder 的应用程序的按钮时实现程序化滑动
- azure-container-service - ACR 服务负责人
- google-play - 谷歌应用订阅按设备而不是用户