简体   繁体   中英

How to check postman response array value when attribute name more than one word

I have the following response to my postman request. I want to check the "Duplicate Subscription" array first item value contains the expected text.

Can you help me with the text, please?

My Response:

{
  "type": "https://httpstatuses.com/409",
    "title": "Conflict",
      "status": 409,
        "detail": "A resource with the specified parameters already exist",
          "correlationId": "f976f20f-c9b1-4822-aa15-2b5e5de3555a",
            "errors": {
    "Duplicated Subscription": [
      "Active subscriptions has reached the limit of 6 for CarrierId=AB, CustomerId=xyz. Cancel existing subscription before subscribing."
    ]
  }
}

This is my test which failed line number 7:

pm.test("Validate conflict Subscription stauts code", function () {
  pm.response.to.have.status(409);
  var jsonData = pm.response.json();
  pm.expect(jsonData).to.have.property("title");
  pm.expect(jsonData).to.have.property("detail");
  pm.expect(jsonData.errors).to.have.property("Duplicated Subscription");
  pm.expect(jsonData.errors).to.have.property("Duplicated Subscription")[0].to.contains("Active subscriptions has reached the limit of 6 for CarrierId=AB, CustomerId=xyz. Cancel existing subscription before subscribing.");
});

use this test unit:

I don't know what kind of unit test you are using so please read the comment and translate it into syntax your unit test supports

pm.test("Validate conflict Subscription stauts code", function(){
  pm.response.to.have.status(409);
  var jsonData = pm.response.json();
  pm.expect(jsonData).to.have.property("title");
  pm.expect(jsonData).to.have.property("detail");
  
  //pm.expect(jsonData.errors).to.have.property("Duplicated Subscription");
  //pm.expect(jsonData.errors).to.have.property("Duplicated Subscription")[0].to.contains("Active subscriptions has reached the limit of 6 for CarrierId=AB, CustomerId=xyz. Cancel existing subscription before subscribing.");

  // i don't know what unit test runner you use so i will use the unit test syntax i am familiar with and explain to you
  pm.expect(jsonData.errors["Duplicated Subscription"]).not.toBeUndefined() // check if this exists . if yes it will !== undefined
  pm.expect(jsonData.errors["Duplicated Subscription"][0]).toEqual("Active subscriptions has reached the limit of 6 for CarrierId=AB, CustomerId=xyz. Cancel existing subscription before subscribing."); // check this is equal ===
});

Found the answer to this.

pm.expect(jsonData.errors).to.have.property("Duplicated Subscription").contains("Active subscriptions has reached the limit of 5 for CarrierId=AB, CustomerId=xyz. Cancel existing subscription before subscribing.");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM