You will need to convert the long code somehow before you make the comparison.
A quick method would be using the JavaScript substr() function like the following…
let expectedResult = "123456";
let code = "1234567890";
let first6 = code.substr(0,6);
console.log(first6);
pm.test("CODE OK", function () {
pm.expect(first6).to.eql(expectedResult);
});
However, pretty sure your code isn’t working as you expect it to.
Your initial code is an object with two elements in it.
Your template is an array with two objects (which each have a single element).
So which code is being evaluated in your test? The first one, the second one, or all of them?
For example, I would expect data.CODE to return as undefined as you are not targeting the array element by its index. data[0].code or data[1].code
If you want to test all of the codes in one go, then you are going to need to make both sets of data use the same format.
Otherwise, you will need to loop though one of the data sets and then compare that way. (Which means both data sets need to be in at least the same order).
** EDIT **
Actually, looking at your first object. You can’t have two elements with the same key, therefore pretty sure the object will only show the last one in the list if you console log it. Which is why your template has it in an array.
I take the first code from the file, transfer it to postman, and compare the answer. Then I take the second code, transfer it to postman and compare the answer.
var json.Data = pm.response.json();
pm.test("CODE OK", function () {
pm.expect(jsonData.CODE).to.eql(data.CODE);
});
I’m glad it works, but can you do me a favour and post a sample of your response.
As this isn’t valid JSON. (You can’t have two elements in an object with the same name).
{
"CODE": "1234567890",
"CODE": "0987654321"
}
I just want to ensure that you are not getting a false positive.
For your “data” template. It’s in an array (which I suspect is what the request is also wrapped in).
[{
"CODE": "123456",
}, {
"CODE": "098765"
}]
data.CODE should return undefined.
Can you try console logging both variables to double check.
console.log(jsonData.CODE); // as you have multiple codes in that object, this should be the value of the last one.
console.log(data.CODE); // this should return undefined as you are not using the array index.
Finally, what is it you are trying to compare. The first code, or do you need to verify both?