4

我正在邮递员中创建一个测试,以检查 JSON 中每个“名称”字段中的关键字“怀孕”。如果 JSON 中的每个“名称”字段都包含关键字,则通过测试,否则失败。

在下面找到我尝试使用的脚本

var jsonData = pm.response.json(); 

var resultCount = jsonData.length;
for (i=0;i<resultCount;i++){
    var modelString = jsonData[i].name;
    if(modelString.indexOf("Pregnancy") > 0)
    {
       tests["Each organisation name field " +i+ " contains   \"Pregnancy\""] = modelString.has("Pregnancy");
    }
    else
    {
       pm.expect.fail("failed");
    }
}
console.log("")

[
    {
        "id": "1-116992830",
        "name": "British Pregnancy Advisory Service      (BPAS)",
        "numberOfLocations": 78
    },
    {
        "id": "1-1560082724",
        "name": "PAMS Pregnancy Ultrasound Centre      (PAMS 3D &     4D Baby Imaging)",
        "numberOfLocations": 1
    },
    {
        "id": "1-2458518720",
        "name": "Pregnancy Ultrasound Ltd      (Babyface4d)",
        "numberOfLocations": 1
    },
    {
        "id": "1-101728376",
        "name": "National Unplanned Pregnancy Advisory Service     Limited",
        "numberOfLocations": 23
    },
    {
        "id": "1-3578030817",
        "name": "Private Pregnancy Ultrasound Services Ltd trading as Expectancy Scanning Studios Ltd      (Expectancy Scanning Studios Ltd)",
        "numberOfLocations": 2
    },
    {
        "id": "1-1412821832",
        "name": "Foundation For Life (Salisbury)      (Pregnancy Advice Salisbury)",
        "numberOfLocations": 1
    },
    {
        "id": "1-2028907839",
        "name": "Miscarriage Clinic Limited      (Centre for     Reproductive Immunolgy and Pregnancy)",
        "numberOfLocations": 1
    },
    {
        "id": "1-744810951",
        "name": "Foundation For Life      (Tyneside Pregnancy Advice Centre)",
        "numberOfLocations": 1
    }
]

我所期待的是,如果每个数组字段中缺少“怀孕”,它应该无法通过测试

4

2 回答 2

4

我将您的 JSON 添加到https://api.myjson.com/bins/13qh7i以创建一个 API 以在 Postman 下进行测试,并在数组中添加了一个额外的对象,其名称中没有“怀孕”一词这样您就可以看到实际的测试。

如果您遍历并检查objects name单词“pregnancy”的属性,并存储在 中找到的那些,hasPregnancy array然后您可以检查API 响应lengtharray的,以查看每个是否确实包含关键字“pregnancy”。lengthobject arrayobject

下面是我在测试中使用的JS代码。我还更新了你的indexOfincludes因为你应该在寻找> -1 with indexOf. 我还将modelString变量应用于toLowerCase()确保name检查不关心大小写等。

在此处输入图像描述

// Get response
var jsonData = pm.response.json();
var resultCount = jsonData.length;

// Test arrays
var hasPregnancy = [];
var doesntHavePregnancy = [];

// Loop through and set arrays with matching data
for (i = 0; i < resultCount; i++) {
  var id = jsonData[i].id;
  var modelString = jsonData[i].name.toLowerCase();
  if (modelString.includes("pregnancy")) {
    hasPregnancy.push({
      "id": id,
      "hasPregnancy": modelString.has("pregnancy")
    });
  } else {
    doesntHavePregnancy.push({
      "id": id
    });
  }
}

// Check that each object in response contained keyword and length matches from test
pm.test("Expect response to contain pregnancy in each object", function() {
  console.log(hasPregnancy);
  console.log(doesntHavePregnancy);
  pm.expect(hasPregnancy.length).to.equal(resultCount);
});
于 2019-05-17T16:04:48.657 回答
1

使用 ES6 的 reduce 函数,在迭代响应时,会累积每个项目的名称是否包含“怀孕”的结果。

const jsonData = pm.response.json();
tests[`Each organisation name field contains \"Pregnancy\"`] = jsonData
  .reduce((acc, org) => acc && org.name.toLowerCase().includes("pregnancy"));
  • 你会发现像 map 和 reduce 这样的 ES6 函数通常更具表现力
  • 使用inlcudes使您的意图比indexOf
  • 我添加了toLowerCase()所以匹配不区分大小写

通过邮递员测试

邮递员测试失败

于 2019-05-17T16:32:41.783 回答