Javascript 获得;未经处理的拒绝承诺”;调用函数时(无法读取未定义的属性';0')
对于我的API测试,我的代码以不同的方式运行Javascript 获得;未经处理的拒绝承诺”;调用函数时(无法读取未定义的属性';0'),javascript,node.js,jasmine,jasmine-node,node-fetch,Javascript,Node.js,Jasmine,Jasmine Node,Node Fetch,对于我的API测试,我的代码以不同的方式运行 在我的第一段代码中,我使用了一个示例API(REST GET请求) 在第二段代码中,我使用我的应用程序API(REST GET请求) 1.我的测试用例中的代码是 describe('PIN Data Verification',()=>{ it('Verify whether 600042 retruns Velacheri PIN data',function(done){ var path=require('pat
describe('PIN Data Verification',()=>{
it('Verify whether 600042 retruns Velacheri PIN data',function(done){
var path=require('path');
let endpoint=require(path.resolve('./config/endpointp.json'));
let pincodes=require(path.resolve('./config/pincodes.json'));
let request=require(path.resolve('./config/postPin.json'));
console.log(request.id,request.Name);
const fetch=require('node-fetch');
let baseUrl=endpoint.url;
let pinCode=pincodes.Vlcy;
get(baseUrl,pinCode)
.then(jsonRes=>{
//console.log(jsonRes);
expect(jsonRes).not.toBeUndefined();
expect(jsonRes.PostOffice[0].Name).toBe('Velacheri');
})
.then(done)
})
})
describe('PIN Data Verification',()=>{
it('Verify whether 600042 retruns Velacheri PIN data',function(done){
var path=require('path');
let endpoint=require(path.resolve('./config/endpointp.json'));
let pincodes=require(path.resolve('./config/pincodes.json'));
let request=require(path.resolve('./config/postPin.json'));
console.log(request.id,request.Name);
const fetch=require('node-fetch');
let baseUrl=endpoint.url;
let pinCode=pincodes.Vlcy;
get(baseUrl,pinCode)
.then(jsonRes=>{
//console.log(jsonRes);
expect(jsonRes).not.toBeUndefined();
expect(jsonRes.PostOffice[0].Name).toBe('Velacheri');
})
.then(done)
})
})
描述('BS_001_retrieveListDN'的验证,()=>{
const fetch=require('node-fetch');
//var testfile=require('./test');
let url,id;
const HttpProxyAgent = require('http-proxy-agent');
const HttpsProxyAgent = require('https-proxy-agent');
let validateResponse = function(response) {
//console.log(response);
return response.json()
.catch(() => {
if (response.status !== 200) {
throw ({
status: response.status,
statusText: response.statusText
});
}
else {
return undefined;
}
})
.then(function(json) {console.log('hi');
console.log(json);
console.log('hi2');
if (response.status !== 200) {
console.log('hi');
throw ({
status: response.status,
statusText: response.statusText,
body: json
});
}
else {
return json;
}
});
};
let get = (url, id) => {
console.log('test');
console.log(url);
console.log(id);
return fetch(url +'/'+ id, {method: 'GET', agent: new HttpProxyAgent('http://10.10.104.4:50683')})
.then(validateResponse);
};
let getR = (url) => {
console.log('test');
//console.log(url);
//console.log(id);
return fetch(url, {method: 'GET', agent: new HttpsProxyAgent('http://10.10.104.4:50683')})
.then(resRaw =>{
console.log(resRaw);
return resRaw.text();
})
.then(resJson=>{
console.log(resJson);
let res=JSON.parse(resJson);
//return res;
console.log(res.Header.ProviderInfo[0].valueType);
return res;
})
};
global.get=get;
global.getR=getR;
当我运行第一个测试用例时,我获得了成功,但是当我运行第二个测试用例时,我得到了以下错误
(node:32740) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '0' of undefined
at getR.then.jsonRes (F:\johny\node_from_home\spec\BS_001_retrieveListDN.spec.js:32:38)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
(node:32740) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:32740) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
F
Failures:
1) Verification of BS_001_retrieveListDN Verify success response for BS_001_retrieveListDN
Message:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
Stack:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
at <Jasmine>
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
Pending:
(节点:32740)未处理的PromisejectionWarning:TypeError:无法读取未定义的属性“0”
在getR.then.jsonRes(F:\johny\node\u from\u home\spec\BS\u 001\u retrieveListDN.spec.js:32:38)
在
在进程中。_tick回调(内部/process/next_tick.js:188:7)
(节点:32740)UnhandledPromisejectionWarning:未处理的承诺拒绝。此错误源于在没有catch块的异步函数内部抛出,或拒绝未使用.catch()处理的承诺。(拒绝id:1)
(节点:32740)[DEP0018]弃用警告:未经处理的承诺拒绝已弃用。将来,未经处理的承诺拒绝将使用非零退出代码终止node.js进程。
F
失败:
1) 验证BS_001_retrieveListDN验证BS_001_retrieveListDN的成功响应
信息:
错误:超时-在jasmine.DEFAULT\u Timeout\u INTERVAL指定的超时内未调用异步回调。
堆栈:
错误:超时-在jasmine.DEFAULT\u Timeout\u INTERVAL指定的超时内未调用异步回调。
在
在ontimeout(timers.js:498:11)
在tryOnTimeout(timers.js:323:5)
at Timer.listOnTimeout(timers.js:290:5)
悬而未决的:
看起来json
对象没有PostOffice
属性。请检查原因。在这两个测试案例中,当承诺被拒绝时,您不会处理该案例。报告错误是因为getR(baseUrl,pinCode)返回了承诺
被拒绝。@t.niese,我不认为这是承诺,但它上的then
方法返回的承诺。尽管如此,如果Header.ProviderInfo
在两个测试中不作为属性存在,您可能是对的,但对我来说似乎不太可能。@trincot是的。这是真的。两行看起来完全相同,所以我把它们混淆了。由于测试用例中的链接而创建的最后一个承诺被拒绝,并且拒绝未被处理。拒绝发生在何处并不重要,重要的是测试用例在链的末尾不处理该承诺。谢谢。这是我的错误。我在第二个案例中没有邮局属性。对不起,tro我是一个初学者,我有一个疑问。考虑函数GRTR。RESULT的值将是我们得到的值:“取(URL,{方法:‘获取’,代理:新的HTTPSPROXIOGER(’)})”????/是RESJSON的值将是“返回RESRAW?文本())中的值;?????是JSONRES(在我的测试用例JS中)的值。将是我们在“return res;”中得到的值???