0

I am learning to write Test cases in postman and my Request body looks like this

{
  "PhoneNumbers": [
   "string"
  ],
  "EmailAddresses": [
    "string"
  ],
  "FirstName": "string",
  "LastName": "string",
}

And my test case is like this

pm.test("Phone number matches", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.PhoneNumbers).to.eql("string");
});

My Response body looks like

{ "PersonId": 1,
  "AdressId": 2, 
  "NationalID number": 123456
}

So basically this API generates an ID for the person given in the request. But my test case is failing with the error "ReferenceError: string is not defined"

I kind of know that it is under an array so I cannot compare like this so Anyone have any suggestion how can I correct this. please advice. Thanks in advance.

anita
  • 7
  • 2
  • Use `jsonData.PhoneNumbers[0]` this type of question would probably be better asked on the Postman community fourm. community.postman.com – Danny Dainton May 05 '21 at 20:37
  • @DannyDainton sorry I will ask in postman community from next time but for this I tried as you mentioned like this pm.expect(jsonData.PhoneNumbers[0]).to.eql("string"); but I got error saying "TypeError: Cannot read property '0' of undefined" – anita May 05 '21 at 20:53
  • Just realised that you mentioned that was your request body, what does the response body look like. Would need that to know what to assert against. – Danny Dainton May 06 '21 at 06:13
  • @DannyDainton here is my response body { "PersonId": 1, "AdressId": 2, } This is what I receive after passing the above mentioned Request. – anita May 06 '21 at 06:25
  • That looks like it's just some of the response. Edit your question and post the full response body, all of it, without that you won't get a solution. There is no point is sharing some or part of it. – Danny Dainton May 06 '21 at 06:35
  • @DannyDainton updated.These are the only 3 fields I am getting in my response. – anita May 06 '21 at 06:52
  • Then those properties are the only things that you can assert against in the response. You can assert again the request body but you don't really have anything in the response to check against. – Danny Dainton May 06 '21 at 07:04

1 Answers1

0

You are trying to access a field from your request in your response. PhoneNumbers is definied in your request body. You can only access PersonId, AdressId and NationalID number.

Testing the fields of your request would be pretty useless since you defined them yourself. You should test if the response is what you expected.

SHell
  • 52
  • 2
  • 8