Trying to assert empty/null/undefined value with no luck

Hello!
I’m fairly new to Postman and I’m not a Mocha expert, I’m using the “Using CSV” data to load a file with the data I need and that is working perfectly, my problem is that I need to assert one of the fields and I had research a lot in Google, StackOverflow, this forum and nothing :frowning:

This is the sample response I get:

[
    {
        "airlineCode": "OLY",
        "airlineCountryCode": "GR",
        "airlineName": "Olympus",
        "arrivalActualDateTime": "2018-07-04T00:37:00",
        "arrivalAirport": "Tenerife Sur Airport (TFS)",
        "arrivalAirportCode": "TFS",
        "arrivalFlightStatus": "Arrived",
        "arrivalFlightStatus2": "Arrived",
        "arrivalScheduledDateTime": "2018-07-03T21:45:00",
        "departureActualDateTime": "2018-07-03T20:00:00",
        "departureAirport": "Hamburg Airport (HAM)",
        "departureAirportCode": "HAM",
        "departureFlightStatus": "Departed",
        "departureFlightStatus2": "Departed",
        "departureScheduledDateTime": "2018-07-03T17:35:00",
        "distance": 3577,
        "flightCode": "OLY246",
        "flightDate": "2018-07-03",
        "flightLegId": 189543873,
        "flightNumber": "246",
        "isArrivalActualDateTimeCalculated": false,
        "isArrivalScheduledDateTimeCalculated": false,
        "isDepartureActualDateTimeCalculated": false,
        "isDepartureScheduledDateTimeCalculated": false,
        "operatingAirlineCode": "OLY",
        "operatingCarrierCode": null,
        "operatingFlightNumber": 246,
        "problemFlightId": null,
        "scheduleId": null
    }
]

From that response, I wanted to assert that the "operatingCarrierCode" is not null or empty so I can cherry-pick from more than 2400 calls the ones that actually have a real value there.
I had tried the following code with no luck:
I know that the field type is “undefined” when I ran some code I found to check it.

//pm.test("To Check if Value is Null", function() {
var jsonData = pm.response.json();
//pm.expect(jsonData.operatingCarrierCode).not.eq('');
//pm.expect(jsonData.operatingCarrierCode).toBeNull();
//expect(context).toBeDefined();
//expect(context).not.toBeNull();
//pm.expect(jsonData.operatingCarrierCode).not.eq(NaN);
//});

//pm.test("operatingCarrierCode present", function() {
   //console.log(typeof jsonData.operatingCarrierCode);
   //pm.expect(jsonData.operatingCarrierCode).to.exist
   //pm.expect(jsonData.operatingCarrierCode).to.not.be.null
   //pm.expect(jsonData.operatingCarrierCode).to.include("null");
//});

pm.test("Body matches string", function () 
{
    pm.expect(pm.jsonData.text()).to.not.include("null");
});

Can anyone be so kind to let me know how to do that? Thanks :smiley:

Hey @amy6586 :wave:

The response is an array so you would need to use jsonData[0].operatingCarrierCode to reference the object within it.

As for assertions, Postman uses chai.js for this and more info on what could be used can be found here:
https://www.chaijs.com/api/bdd/

There are some test examples here on our learning center that also might help:

This is also a similar issue covered by @vdespa in one of his YouTube videos:

1 Like

hi @dannydainton,

Thanks for your reply I had tried the following piece of code but is not working as expected.

pm.test("Verify the operatingCarrierCode is not null"  , function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData[0].operatingCarrierCode, 'operatingCarrierCode is not available').to.not.be.undefined;
});

In my JSON the field operatingCarrierCode comes as null and sometimes it will have an airline code and checking for undefined does not work I had tried using something like:

pm.test("Verify the operatingCarrierCode is not null"  , function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData[0].operatingCarrierCode, 'operatingCarrierCode is not available').to.not.be.undefined;
});

Screenshot 2021-10-21 at 16.36.14

but the response is still shown as null and the assertion passes, I want to check if the response contains something different as null it passes.

if I use something like this:

ppm.expect(pm.jsonData[0].operatingCarrierCode).to.not.include("null");

I get an error

‘TypeError: Cannot read property ‘0’ of undefined’

any advice?

thanks :slight_smile:

If it’s not null then it’s a string with the code, that wouldn’t be undefined unless the whole property was removed from the response.

Does this not work? If it is null or any other type then it will fail.

pm.test("Verify the operatingCarrierCode is returned"  , function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData[0].operatingCarrierCode, 'operatingCarrierCode is not available').to.be.a('string');
});

Ideally, you would be asserting against an actual known value in the response, as this would pass if the carrier code was Bananas or some other string value.

Maybe this was a typo but none of that really makes sense to me. You wouldn’t use pm.jsonData and the to.not.include() wouldn’t work like that, it would also be looking for a string containing null and not the datatype.

Thanks @dannydainton that works like a charm, unfortunately, this value can be any airline. code so there are 100s of possibilities but checking if it is a string works very well.

:slight_smile:

1 Like

You mentioned that you’re using a CSV file, does this contain those codes?

Or are you using the data in the file for something else?

@dannydainton the CSV is only to change the date and flight number in the query parameters

1 Like