FAIL: Check that the user representation is valid | AssertionError: expected undefined to be a string

Hi everyone! I’m new to Postman and I have a question:

This is the error message I get:
“Check that the user representation is valid | AssertionError: expected undefined to be a string”

While my code works with my other tests.
I wonder if it’s not because my answer contains several objects…

As the comments show /* */, I tried several alternatives, to no avail.

Thank you in advance for your help! :pray:

Mathilde

To all those who wonder:
All you had to do was loop over the array elements with a forEach() method to make it work!

My recommendation would be to put the test(s) within the loop.

I do mean tests. You currently have one test with lots of assertions in a loop.

pm.test is the test and the pm.expect are the assertions.

I recommend to break down your asserts into individual tests.

Otherwise what will happen is that if any assertion fails, then the test will fail immediately and the rest of the assertions will not be evaluated. If the firstname fails, then nothing else will be checked.

I loathe to say its best practice, but try and create tests that are singular as possible. If you want the feedback from all of your assertions, then they should be separate tests.

When crafting tests in a loop, you can use backticks\string literals to craft custom test names based on the variables. So the test name is more relevant.

JavaScript Template Literals (w3schools.com)

For example.

pm.test(`custom test case name ${variable}`, function...

Have a look at the following for a further example.

Test Body response by looping through Array or If/Else - Just getting started - Postman