0

对于我的 API 测试,我的代码以不同的方式表现。我正在使用带有节点 js 的 jasmine 框架。

  1. 在我的第一个代码中,我使用了一个示例 API(REST GET 请求)
  2. 在我的第二个代码中,我正在使用我的应用程序 API(REST GET 请求)

1.我的测试用例中的代码是

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)

    })


})
  1. 我的第二个测试用例中的代码是

    describe('BS_001_retrieveListDN 验证',()=>{

        it('Verify success response for BS_001_retrieveListDN',function(done){
            var path=require('path');
            let endpoint=require(path.resolve('./config/endpoint.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 apikey=endpoint.apikey;
            let userID=endpoint.userID;
            let quantity=endpoint.quantity;
            let operatorId=endpoint.operatorId;
            let orderNumber=endpoint.orderNumber;
            let numberCategory=endpoint.numberCategory;
        let fullUrlWithQueryParameters= baseUrl + "?apikey=" + apikey + "&userID=" + userID + "&quantity=" + quantity + "&operatorId=" + operatorId + "&orderNumber=" + orderNumber + "&numberCategory=" + numberCategory
            //let pinCode=pincodes.Vlcy;
            console.log(fullUrlWithQueryParameters);
            console.log("test");
            getR(fullUrlWithQueryParameters)
            .then(jsonRes=>{
                //console.log(jsonRes);
                expect(jsonRes).not.toBeUndefined();
                expect(jsonRes.PostOffice[0].Name).toBe('Velacheri');
            })
            .then(done)
    
        })
    
    })
    

对于 1 和 2,我使用的常见 get 和 getR 函数如下

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:
4

0 回答 0