【问题标题】:How to assert array in array?如何在数组中断言数组?
【发布时间】:2021-03-10 23:41:28
【问题描述】:

有人可以帮我在邮递员的数组中断言数组吗?

我有这样的代码和平:

"documents": [
        {
            "fileName": "file01_guid.pdf",
            "documentType": "document",
            "parentFiles": [
                "file01.pdf"
            ]
        },
        {
            "fileName": "file02_guid.pdf",
            "documentType": "document",
            "parentFiles": [
                "file01.pdf"
            ]
        }

我需要使用这种方法断言“ParentFiles”数组:

var array = [];
var range = (json_response.data.documents).length

for (var i = 0; i < range; i++)

{
 var file = json_response.data.documents[i].fileName
 var type = json_response.data.documents[i].documentType;

 array.push(file)
 array.push(type)
}

所以我可以写这样的测试:

{
    pm.expect(array).to.include("file01.pdf", "file01.pdf");
});

提前谢谢你

【问题讨论】:

  • 你想断言什么?为什么还要 pushind 文档类型?
  • 我想使用“include”函数声明 parentFiles 数组,就像我在帖子中描述的那样。我有点卡住了
  • 你想声明每个对象的 parentFile 数组,或者你想用一些数组声明新数组
  • 如果有两种方法可以实现我的目标,那么我想知道两者。

标签: javascript json testing postman assertion


【解决方案1】:

您可以通过简单的过滤和检查长度来验证"file01.pdf" 是否存在。您还可以通过减少来更有效地构建您的array

const json_response = {
  "data": {
    "documents": [{
      "fileName": "file01_guid.pdf",
      "documentType": "document",
      "parentFiles": ["file01.pdf"]
    }, {
      "fileName": "file02_guid.pdf",
      "documentType": "document",
      "parentFiles": ["file01.pdf"]
    }]
  }
};

const array = json_response.data.documents.reduce((acc, doc) => {
  const { fileName, documentType, parentFiles: [ pfName ] } = doc;
  return [ ...acc, fileName, documentType, pfName ];
}, []);

console.log(array);

// Exactly two instances of "file01.pdf" exist.
console.log(array.filter(val => val === "file01.pdf").length === 2);
.as-console-wrapper { top: 0; max-height: 100% !important; }

如果你只是想比较数组,你可以尝试以下方法:

const json_response = {
  "data": {
    "documents": [{
      "fileName": "file01_guid.pdf",
      "documentType": "document",
      "parentFiles": ["file01.pdf"]
    }, {
      "fileName": "file02_guid.pdf",
      "documentType": "document",
      "parentFiles": ["file01.pdf"]
    }]
  }
};

const allEqual = json_response.data.documents
  .every(({ parentFiles: [ filename ] }) => filename === "file01.pdf");

console.log(allEqual);
.as-console-wrapper { top: 0; max-height: 100% !important; }

【讨论】:

  • 你为什么不能只使用 lodash ?
  • @PDHide 因为 JavaScript 已经发展,依赖项是额外的开销。
  • @PDHide 该代码也适用于 NodeJS,它不是 DOM 操作(或依赖)代码。
  • @Mr.Polywhirl 最后我决定使用你的方法:const array = json_response.data.documents.reduce((acc, doc) =&gt; { const { fileName, documentType, parentFiles: [ pfName ] } = doc; return [ ...acc, fileName, documentType, pfName ]; }, []); 因为我的响应,文件可以不同。我研究了你的方法,它完全符合我的需要。谢谢
【解决方案2】:

您可以使用 foreach 循环并比较值

//store expected files in order
array=["file01.pdf","file01.pdf"]

//now forEach on each element
json_response.data.documents.forEach( (elem,index,arr)=>{
pm.expect(elem.parentFiles[0]).to.be.eql(array[index])
})

如果您只想比较两个数组,那么:

pm.expect(_.isEqual(array,array2)).to.be.true

lodash isEqual 可用于比较两个类似数组的对象

【讨论】:

  • 我不明白你的“循环”选项。当我尝试过时:pm.test('parentFiles equal "BR_21080_file18.pdf"', function() { array=["file01.pdf","file01.pdf"] pm.response.json().documents.forEach( (elem,index,arr)=&gt;{ pm.expect(elem.parentFiles[0]).to.be.eql(array[index]) })}); 我收到“parentFiles equal”file18.pdf | TypeError: Cannot read property 'forEach' of undefined"
  • 是更新后的代码json_response.data.documents
猜你喜欢
  • 1970-01-01
  • 2015-04-11
  • 2015-03-27
  • 2020-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多