1

I am new to the working with TAPE JS for testing. I have it all setup and working, and it works fine with regular tests. But I am trying to test a unique REST API based product that relies on certain calls to have been made before the next call has the information needed to have a successful call.

So here are the first two calls I am trying to get working:

var SessionId;

test('beginIqsSession', function (assert) {
    assert.plan(1);
    var requestData = {"ProductDataArray":{"Feid":"GIQNY","AltData":"SaneID:null","Debug":"false","PageId":"1.1"}};

    request({
    url: 'http://192.168.99.100/Iqs/api.php/beginIqsSession',
    method: "POST",
    json: requestData
    }, function(error, response, json){

        if(json.responseDataPayload.SessionId)
        {
            SessionId = json.responseDataPayload.SessionId;

            assert.equal(1,1);
        }
    });
    assert.end();
});


test('validateAddress', function (assert) {
    assert.plan(2);

    console.log("Retrieving validateAddress response");

    var requestData = {"SessionId":SessionId,"ValidateAddressDataArray":{"PropertyStreetNumber":"20671","PropertyStreetName":"mountain vista dr","PropertyCity":"anchorage","PropertyState":"AK","PropertyZipCode":"99577"}};

    console.log(SessionId);

    request({
        url: 'http://192.168.99.100/Iqs/api.php/validateAddress',
        method: "POST",
        json: requestData
    }, function (error, response, body) {

        if (!error) {
            console.log(body);
        }
        else {
            console.log("error: " + error)
        }
    });

    assert.end();
});

So basically in the code above, I am trying to test beginIqsSession, wait for its response, and store a piece of data from that response that future calls require to be sent in.

in validateAddress you'll see I am trying to pass SessionId in which was returned in the previous call, but because this test is being run at the same time as the previous test, this variable is still empty. How can I make the second call, and all future calls, to wait for the previous call to run?

assert.plan apparently doesn't work in this way.

Dylan Cross
  • 5,918
  • 22
  • 77
  • 118
  • 2
    You should be putting the `assert.end()` of the first test inside the request callback. Secondly, you shouldn't have tests that depend on the results of a previous test. Just make it one test. – idbehold Sep 26 '16 at 19:36
  • Ah yes, that makes sense - works good like that. And yeah, totally aware that each test should do just one thing on its own, but we have a fairly large API to test, but we have that in mind. – Dylan Cross Sep 26 '16 at 19:43

1 Answers1

1

You could use the Promise API

var SessionId;

let p1 = new Promise((resolve, reject) => {
  test('beginIqsSession', function (assert) {
    assert.plan(1);
    var requestData = {"ProductDataArray":{"Feid":"GIQNY","AltData":"SaneID:null","Debug":"false","PageId":"1.1"}};

    request({
    url: 'http://192.168.99.100/Iqs/api.php/beginIqsSession',
    method: "POST",
    json: requestData
    }, function(error, response, json){

        if(json.responseDataPayload.SessionId)
        {
            SessionId = json.responseDataPayload.SessionId;

            assert.equal(1,1);
          resolve(SessionId);
        }
    });
    assert.end();
  });
})

p1.then((SessionId) => {
  test('validateAddress', function (assert) {
    assert.plan(2);

    console.log("Retrieving validateAddress response");

    var requestData = {"SessionId":SessionId,"ValidateAddressDataArray":{"PropertyStreetNumber":"20671","PropertyStreetName":"mountain vista dr","PropertyCity":"anchorage","PropertyState":"AK","PropertyZipCode":"99577"}};

    console.log(SessionId);

    request({
        url: 'http://192.168.99.100/Iqs/api.php/validateAddress',
        method: "POST",
        json: requestData
    }, function (error, response, body) {

        if (!error) {
            console.log(body);
        }
        else {
            console.log("error: " + error)
        }
    });

    assert.end();
  });
  
});
Tom Goldenberg
  • 566
  • 3
  • 6
  • 14