当我尝试断言其中包含数组的整个 json 时,仅包含不起作用

contains only is not working when I try to assert the entire json which has arrays inside it

以下是回复

  {  "agentID": null,
    "browserData": null,
    "channel": "CABLEDOTCOM",
    "confidenceLevel": "HIGH",
    "creditCustomAttributes": [
        {
            "name": "accountNumber",
            "value": "8009001002003015"
        },
        {
            "name": "alternatePhoneNumber",
            "value": "7098863456"
        },
        {
            "name": "customerType",
            "value": "Residential"
        },
        {
            "name": "ezEnrollCreditNoCredit",
            "value": "NO"
        },
        {
            "name": "houseNumber",
            "value": "34"
        },
        {
            "name": "inboundPhoneNumber",
            "value": "9079963456"
        },
        {
            "name": "installationDate",
            "value": "12-12-2018"
        },
        {
            "name": "installationTimeSlot",
            "value": "11AM"
        },
        {
            "name": "installationType",
            "value": "self"
        },
        {
            "name": "lineOfBusinessOrdered",
            "value": "video,hsd"
        },
        {
            "name": "mrcFromOrder",
            "value": ""
        },
        {
            "name": "operatorId",
            "value": "9097"
        },
        {
            "name": "salesId",
            "value": "99998"
        },
        {
            "name": "timezone",
            "value": "EST"
        },
        {
            "name": "workOrderDate",
            "value": "12-12-2018"
        },
        {
            "name": "workOrderNumber",
            "value": "13-00-11-990-2018"
        }
    ],
    "csgLocationID": null,
    "customerDetails": {
        "billingAddress": {
            "addressLine1": "PO BOX 445",
            "addressLine2": "Apt 345",
            "city": "AO",
            "country": "USA",
            "state": "AE",
            "zipCode": "09061"
        },
        "currentAddress": {
            "addressLine1": "PO BOX 445",
            "addressLine2": "Apt 345",
            "city": "AO",
            "country": "USA",
            "state": "AE",
            "zipCode": "09061"
        },
        "dateOfBirth": "1975-01-01",
        "driversLicense": {
        },
        "name": {
            "firstName": "JOHN",
            "lastName": "BREEN",
            "middleName": "WAKEFIELD"
        },
        "shippingAddress": {
            "addressLine1": "PO BOX 445",
            "addressLine2": "Apt 345",
            "city": "AO",
            "country": "USA",
            "state": "AE",
            "zipCode": "09061"
        },
        "socialSecurityNumber": "666436878",
        "telephoneNumber": "7818945369"
    },
    "customerID": null,
    "customerType": "RESIDENTIAL",
    "hitPreference": "HIT_WITH_NARRATIVE",
    "installationType": null,
    "language": null,
    "lineOfBusiness": null,
    "marketID": "1111",
    "matchPreference": "EXACT_ONE",
    "orderID": null,
    "verificationStatus": {
        "bureauResponse": null,
        "failureReasons": [
            {
                "code": "ERROR",
                "message": "Retriable Server Error. Response Code: 503, Response Message: Service Unavailable"
            },
            {
                "code": "R0201",
                "message": "Workflow Complete."
            }
        ],
        "status": "SUCCESS"
    },
    "workOrderNumber": null
}

以下是QA_Schema

   { "agentID": null,
    "browserData": null,
    "channel": "CABLEDOTCOM",
    "confidenceLevel": "HIGH",
    "creditCustomAttributes": [
        {
            "name": "lineOfBusinessOrdered",
            "value": "video,hsd"
        },
        {
            "name": "timezone",
            "value": "EST"
        },
        {
            "name": "alternatePhoneNumber",
            "value": "7098863456"
        },
        {
            "name": "mrcFromOrder",
            "value": ""
        },
        {
            "name": "workOrderDate",
            "value": "12-12-2018"
        },
        {
            "name": "installationDate",
            "value": "12-12-2018"
        },
        {
            "name": "houseNumber",
            "value": "34"
        },
        {
            "name": "accountNumber",
            "value": "8009001002003015"
        },
        {
            "name": "ezEnrollCreditNoCredit",
            "value": "NO"
        },
        {
            "name": "customerType",
            "value": "Residential"
        },
        {
            "name": "installationType",
            "value": "self"
        },
        {
            "name": "salesId",
            "value": "99998"
        },
        {
            "name": "inboundPhoneNumber",
            "value": "9079963456"
        },
        {
            "name": "workOrderNumber",
            "value": "13-00-11-990-2018"
        },
        {
            "name": "installationTimeSlot",
            "value": "11AM"
        },
        {
            "name": "operatorId",
            "value": "9097"
        }
    ],
    "csgLocationID": null,
    "customerDetails": {
        "age": "#ignore",
        "billingAddress": {
            "addressLine1": "PO BOX 445",
            "addressLine2": "Apt 345",
            "city": "AO",
            "country": "USA",
            "state": "AE",
            "zipCode": "09061"
        },
        "currentAddress": {
            "addressLine1": "PO BOX 445",
            "addressLine2": "Apt 345",
            "city": "AO",
            "country": "USA",
            "state": "AE",
            "zipCode": "09061"
        },
        "dateOfBirth": "##string",
        "driversLicense": {
        },
        "name": {
            "firstName": "JOHN",
            "lastName": "BREEN",
            "middleName": "WAKEFIELD"
        },
        "shippingAddress": {
            "addressLine1": "PO BOX 445",
            "addressLine2": "Apt 345",
            "city": "AO",
            "country": "USA",
            "state": "AE",
            "zipCode": "09061"
        },
        "socialSecurityNumber": "#ignore",
        "telephoneNumber": "7818945369"
    },
    "customerID": null,
    "customerType": "RESIDENTIAL",
    "hitPreference": "HIT_WITH_NARRATIVE",
    "installationType": null,
    "language": null,
    "lineOfBusiness": null,
    "marketID": "1111",
    "matchPreference": "EXACT_ONE",
    "orderID": null,
    "verificationStatus": {
        "bureauResponse": null,
        "failureReasons": [
            {
                "code": "ERROR",
                "message": "Retriable Server Error. Response Code: 503, Response Message: Service Unavailable"
            },
            {
                "code": "R0201",
                "message": "Workflow Complete."
            }
        ],
        "status": "SUCCESS"
    },
    "workOrderNumber": null
}

如果你手动比较这两个,两者是完全一样的。但唯一的区别是顺序。两者的顺序不同。

所以当我这样做时

不匹配。

如果没有办法,是不是要先把数组排序,然后再做断言?

当你有嵌套时,总是尝试在 JSON 的部分上做一个 match。怎么分配由你决定。

我已将您的复杂转储简化为一个简单示例。但是你会在这里得到这个想法:

Feature:

  Scenario:
    * def response =
    """
    {
      "channel": "CABLEDOTCOM",
      "creditCustomAttributes": [
        {
            "name": "timezone",
            "value": "EST"
        },
        {
            "name": "lineOfBusinessOrdered",
            "value": "video,hsd"
        }
      ]
    }
    """

    * def expected =
    """
    {
      "channel": "CABLEDOTCOM",
      "creditCustomAttributes": "#array",
    }
    """

    * match response == expected
    * match response.creditCustomAttributes contains only
    """
    [
      {
          "name": "lineOfBusinessOrdered",
          "value": "video,hsd"
      },
      {
          "name": "timezone",
          "value": "EST"
      }
    ]
    """

如果需要,请花一些时间研究其他答案:

@GopalSubramanian - 您可以通过两种方式做到这一点:

  1. 如果您不关心数组中的值,请使用模式验证。
....
* match response.creditCustomAtributes.#[]
## OR is you also want to check the size of arry but do not care about values then 

* match response.creditCustomAttributes.#[10] ## ensures that there are 10 array values

在此处查看文档 - https://github.com/karatelabs/karate#schema-validation

  1. 如果您 DO 想要根据预期的有效负载验证数组中的值,则使用 contains any。我看到@PeterThomas 已经向您展示了一个例子。这是文档 - https://github.com/karatelabs/karate#match-contains-any