I want to do message field validation but i am getting


2

Here are my JSON body and tests. how do i validate the message field that is “Add data success”
Please help anyone knows.

The message is a bit misleading, as it appears to be reading the expected and actual result correctly.

I think its because the assertion is worded slightly incorrectly.

Shouldn’t it be…

pm.expect(jsonData.msg).to.equal("Add data success"); // not is.to.equal

Thank you for your reply. It’s not working. Now what should i do? Please let me know?

That is different than before.

Before your code was executing, but you weren’t getting the correct result.

The error you are now receiving seems to indicate that it can’t execute the tests. (Perhaps because your pm.expect is not wrapped in a test.

Could you please share all of the code in your Tests tab? Just in case something else is interfering with the test.

Hey, here is my Json Code…

{
“Permanent_Address”:
{
“House_Number”: “40/4”,
“City”: “Dhaka”,
“State”: “Dhaka”,
“Country”: “Bangladesh”,
“PhoneNumber”: [
{
“Std_Code”: “1162090”,
“Home”: “Tower Twenty Four”,
“Mobile”: “+8801746684953”
},
{
“Std_Code”: “1110338426”,
“Home”: “Tower twenty four”,
“Mobile”: “+8801913903626”
}
]
},
“stId”: 4177479
}

And here is my test code -

pm.test(“Status field value validation”, function () {
pm.expect(jsonData.status).to.eql(“true”);
});

pm.test(“Message field value validation”, function () {
pm.expect(jsonData.msg).to.eql(“Add data success”);
});

please look where is my mistake. And provide me some suggestion. Thanks.

That can’t be all of the test code, as I can’t see where you are parsing the response to the variable jsonData. Can you please include all of the code (and please use the preformatted text option in the editor so all of the code doesn’t get aligned to the left).

The following code seems to work fine.

jsonData = pm.response.json();

pm.test("Status field value validation", function () {
    pm.expect(jsonData.status).to.eql("true");
});

pm.test("Message field value validation", function () {
    pm.expect(jsonData.msg).to.eql("Add data success");
});

image

For troubleshooting purposes, duplicate the request, and paste the above into the Tests tab and remove anything else. Does it work then?

1 Like
  1. Write a test case for Status Code Validation. Create Student Address
    METHOD: POST
    STATUS CODE: 200
    Base_Url: https://thetestingworldapi.com
    Request URL: baseUrl/api/addresses
    Body
    { “Permanent_Address”: { “House_Number”: “sample string 1”,“City”: “sample string 2”, “State”: “sample string 3”, “Country”: “sample string 4”,“PhoneNumber”: [
    { “Std_Code”: “sample string 1”,“Home”: “sample string 2”, “Mobile”: “sample
    string 3”
    },{ “Std_Code”: “sample string 1”,“Home”: “sample string 2”, “Mobile”: “sample
    string 3”
    }
    ]
    },
    “stId”: id
    }
    Response: {
    “status”: “true”,
    “msg”: “Add data success”
    }
  2. Write a test case for Status Code Validation. 3. Write a test case for status and message

Hey, how are you?
Actually, this was my task and I want to validate the message field from the response data. but I can’t verify. I don’t know what’s the problem on my code. I have searched on google but I can not reach a specific result. please help if you can. Thank you.

Have you tried using the code I provided in the previous comment.

Make sure you don’t anything else in the test tab.

It should work.

1 Like

The reason the second test is failing is because of the response.

It’s “Add data success” and has two spaces between Add and data.

Even this editor keeps removing the second space thinking its wrong. So its hard to spot at a glance. It’s more obvious if you see the screenshot.

image

pm.test("Message field value validation", function () {
    pm.expect(jsonData.msg).to.eql("Add  data success");
});
1 Like

Thank you very much. Now it’s work. Thanks a lot.
Take love