我是NodeJs的新手,正在尝试运行测试。我正在尝试从get调用返回http响应,以便可以对其运行断言。SendGetResponse()返回promise,sendResponse等待这个promise的实现。但我得到(node:2152) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'then‘of undefined,sendResponse不等待。
任何帮助都将不胜感激!
async sendResponse(){
this.setUpRequest();
if (this.verb == 'get'){
var Promise1 = await this.sendGetRequest();
console.log("Going to resolve promise");
console.log(Promise1);
}
console.log('Leaving Sendresponse');
return this.ResponseMessage;
}
sendGetRequest() {
console.log("In SendGetRequest()");
const SendRequest = this.GetRequest(this.path,this.headers);
SendRequest.then(function(res,err){
return new Promise(function(resolve, reject){
if (err == null){
console.log("Resolving promise");
resolve(res.text);
}
else{
console.log("Rejecting promise");
reject(err);
}
}).catch(error => {
console.log('Exception caught', err.message);
});
});
}
GetRequest(path,headers){
console.log("In GetRequest()");
return chai.request(baseurl).get(path).set(headers);
}发布于 2019-04-04 02:18:33
查看chai Requests documentation,如果要设置任何HTTP属性,如标题或表单数据,看起来需要调用.send()。尝试对GetRequest()进行以下更改
function GetRequest(path, headers) {
return chai.request(baseUrl)
.get(path)
.set(headers)
.send();
}https://stackoverflow.com/questions/55501278
复制相似问题