gpt4 book ai didi

javascript - JS Tape - 等待上一个异步测试完成以进行下一个测试

转载 作者:太空宇宙 更新时间:2023-11-04 00:34:30 26 4
gpt4 key购买 nike

我刚开始使用 TAPE JS 进行测试。我已经完成所有设置并正常工作,并且在常规测试中工作得很好。但我正在尝试测试一种独特的基于 REST API 的产品,该产品依赖于在下一次调用获得成功调用所需的信息之前进行的某些调用。

所以这是我试图开始工作的前两个电话:

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();
});

所以基本上在上面的代码中,我尝试测试 beginIqsSession,等待其响应,并存储该响应中的一段数据,以便将来的调用需要发送。

在 validateAddress 中,您会看到我正在尝试传递上一次调用中返回的 SessionId,但由于此测试与上一个测试同时运行,因此该变量仍然为空。如何进行第二个调用以及所有以后的调用,以等待上一个调用运行?

assert.plan 显然不能以这种方式工作。

最佳答案

您可以使用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();
});

});

关于javascript - JS Tape - 等待上一个异步测试完成以进行下一个测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39710883/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com