Hello, I’m currently learning test using Postman and I’m not an expert with Javascript. I am trying to create a function to test, but using it in pm.test throws error instead of failing the test case:
There was an error in evaluating the test script: AssertionError: expected null not to be null
Below is the test that throws AssertionError:
var jsonData = pm.response.json();
var items = jsonData.data.items;
function checkValues(object) {
for (const value of Object.values(object)) {
pm.expect(value).to.not.be.null;
};
};
items.forEach((item) => pm.test("item values are not null", checkValues(item)));
but if I don’t define a separate function, it fails the test as expected:
var jsonData = pm.response.json();
var items = jsonData.data.items;
items.forEach((item) => pm.test("item values are not null", function() {
for (const value of Object.values(item)) {
pm.expect(value).to.not.be.null;
};
}));
Can someone help me and explain what should I do if I need that separate function so that I can reuse it? Thank you.