我正在寻找一个新的框架,我遇到了chakram,现在我尝试做以下测试:
执行一个api调用(get),这将返回一个元素数组,我需要对这些元素进行迭代,并使用它的Id进行另一个api调用,然后断言内容。下面是该数组的示例。
[
{
"id": "1",
"user": "user",
},
{
"id": "2",
"user": "user",
},
{
"id": "3",
"user": "user",
},
{
"id": "4",
"user": "user",
},
{
"id": "5",
"user": "user",
}
]我所不能做的是使用响应中的每个id进行另一个api调用。
这是我的测试:
describe("Call registered user", function(){
it("Validate all user data is ok", function(){
this.timeout(25000)
return chakram.get(config.environment.url)
.then(function(response){
//console.log(JSON.stringify(response,null, 4));
for(var i=0; i < response.length; i++ ){
console.log(config.environment.url+"/"+response.data[i].id);
return chakram.get(config.environment.url+"/"+response.data[i].id)
.then(function(userData){
console.log(i);
expect(userData.response.statusCode).to.equal(200)
return chakram.wait();
});
}
})
});
});问题是测试没有达到for。有人能指出我哪里做错了吗?顺便说一句,我是JS的新手。
发布于 2019-01-18 22:43:34
首先,我想指出,.then必须一个接一个地出现。
您可以对每个测试使用一个it("", () => {})。
所以,
let testArray;
function testFunction() {
return chakram.get(config.environment.url)
.then(response => {
testArray = response;
})
}
describe("Call registered user", function(){
testFunction();
testArray.map( user => {
it("Validate userId: " + user.id, () => {
return chakram.get(config.environment.url+"/"+user.id)
.then(userData => {
console.log(user);
expect(userData.response.statusCode).to.equal(200)
return chakram.wait();
});
});
})
});尝试这种方式会更准确。
https://stackoverflow.com/questions/51680820
复制相似问题