【问题标题】:Karate; Object type not being throws TypeError as "not an Object"空手道;对象类型未抛出 TypeError 作为“不是对象”
【发布时间】:2019-03-06 16:21:31
【问题描述】:

通过我的测试,我希望对数组中的 json 对象进行一些评估。示例如下所示:

"sections": [
  {
    "internal_id": 10635,
    "uuid": "a56357fa-fbe6-BARS-89d4-8e8fdbda18b0",
  },
  {
    "internal_id": 10636,
    "uuid": "33a4c634-BARS-4b20-ac3d-54370b32d989",
  },
  {
    "internal_id": 10637,
    "uuid": "99443ce8-5007-4ec2-BARS-b459df30d33b",
  },
  {
    "internal_id": 10638,
    "uuid": "4a54a868-1bfa-BAZZ-93b1-d95a4b529cdf",
  }
]

在进行一些调试时,我这样做了:

* print typeof(sections[0]) => 这回来说object

我试图评估这样做的单个对象:

* eval Object.keys(sections[0]).length; => 这会返回 TypeError: <object_at_index_0> is not an Object in <eval>

我在 JS 方面没有太多经验,但是有人可以帮我解决这个问题吗?

【问题讨论】:

  • 你想从这个 JSON 数组中验证什么??
  • @BabuSekaran 都是你的 :)
  • @BabuSekaran,我想验证对象的长度是否为 2。或者,基本上验证 JSON 数组中的每个对象的大小是否相同。在我写出断言之前的调试中,这让我想到了为什么即使该类型被列为对象,我也无法评估该对象

标签: automation karate


【解决方案1】:

您可以为单个 JSON 对象编写预期架构,然后使用 match each 验证 JSON 数组中的所有数据。

* def sectionArray =
"""
{
"sections": [
  {
    "internal_id": 10635,
    "uuid": "a56357fa-fbe6-BARS-89d4-8e8fdbda18b0",
  },
  {
    "internal_id": 10636,
    "uuid": "33a4c634-BARS-4b20-ac3d-54370b32d989",
  },
  {
    "internal_id": 10637,
    "uuid": "99443ce8-5007-4ec2-BARS-b459df30d33b",
  },
  {
    "internal_id": 10638,
    "uuid": "4a54a868-1bfa-BAZZ-93b1-d95a4b529cdf",
  }
]
}
"""
* match each sectionArray.sections == {"internal_id":"#present","uuid":"#present"}

您还可以通过以下方式验证数组的大小,

* match sectionArray.sections == "#[4]"

参考Fuzzy Matching

【讨论】:

  • 好的,所以,据我所知,通过在两个键上匹配"#present",这断言如果有一个键或者每个对象有> 2个键,它将失败?
  • 每个 JSON 对象应该只有 internal_iduuid 键,任何其他情况都会失败。
猜你喜欢
  • 2019-04-06
  • 1970-01-01
  • 1970-01-01
  • 2021-02-10
  • 2021-02-15
  • 2020-11-19
  • 2018-08-06
  • 2016-10-12
  • 1970-01-01
相关资源
最近更新 更多