我对NodeJ很陌生,正在尝试运行测试。我试图从get调用返回http响应,以便可以在其上运行断言。 SendGetResponse()返回承诺,并且sendResponse等待此承诺被兑现。但是我得到(node:2152)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性'then',并且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);
}
答案 0 :(得分:0)
从chai
Requests documentation看,如果设置任何HTTP属性(例如标头或表单数据),则似乎需要调用.send()
。尝试对GetRequest()
进行以下更改:
function GetRequest(path, headers) {
return chai.request(baseUrl)
.get(path)
.set(headers)
.send();
}