Comparing array values

I am verifying if my values match with JSON response. Here is my code -

pm.test("Status test", function () {
    pm.response.to.have.status(200);
});

pm.test("Testing Related Products", function () {    
    let responseJSON = pm.response.json();
    
    if (responseJSON.contains.length > 0) {
        let expectedStrings = pm.variables.get("related_prod_ids").split(",");
        console.log("Expected String -",expectedStrings); //["111537", "111345"]
        let actualString = responseJSON.contains[0].relatedProducts.map(product => product.productCode.toString());
        console.log("Actual String -", actualString);//["111537"]
                
        if (pm.expect(actualString).to.have.members(expectedStrings)) {
            console.log("Testcase Passed!");
        } else {
            console.log("One or more Related Products did not match...!");
        }
    } else {
        console.log("Empty response...");
    }
});

Query - I get an error - Testing Related Products | AssertionError: expected [ ‘111537’ ] to have the same members as [ ‘111537’, ‘111345’ ]
But Console never shows - console.log(“One or more Related Products did not match…!”);
What am I doing wrong?

Hey @ruma204 :wave:t3:

Your second if statement contains an assertion function and not a conditional evaluation so it wouldn’t work in that statement.

That assertion should be inside the test function and will give you a yes/no answer against what you’re checking. I don’t see the need to have a nested if / else that writes a console log for that.

1 Like