By the way, I have two questions about my code - how to sort an array in Postman and if there is a better way to compare objects except mine. I use this block to compare properties to check equality of JSON objects body1 and body2 (I’m interested in equality of particular set of properties, f. e. body1 can have property “id” and body2 not, so it’s not about strict equality).
@singhsivcan Yes, sure, but I can’t share request, so I will try to provide all code that needed.
Declaring of test function:
pm.environment.set("tests", function tests() {
var tests = {},
// technically it is not equality but check that body1 *contains* listed properties equal to body2 properties
tests.equals = function equals(body1, body2, properties_list) {
properties_list.forEach((property) =>
pm.expect(body1).to.have.deep.property(property, body2[property])); //doesn't work for arrays
};
return tests;
} + '; tests();');
Why not keep it simple and make another function for arrayTest instead of reusing the one for object?
function checkArr(body1, body2) {
let arr1 = body1.arr.sort(), //javascript has this in-built for arrays
arr2 = body2.arr.sort();
pm.expect(arr1).to.eql(arr2);
}
Thanks, that works! I’ve tried the same solution but without separate function to check and I seem to mistake somewhere. Your way works fine and I will check for mistakes in my similar solution.
Maybe, it was wrong to call body1[property].sort() or (body1[property]).sort() like I tried. Maybe, that was the matter.