Need help creating assertions for test

Iā€™m new to Postman and do not have any coding experience. How would I go about creating assertions to verify values for ā€œnameā€, ā€œlatitudeā€, ā€œupperLimitā€ and ā€œlowerLimitā€ in the code below?

{
ā€œ@typeā€: ā€œAntennaConfigā€,
ā€œidā€: ā€œ9e22f537-0f68-4caf-9d98-445555578d27ā€,
ā€œnameā€: ā€œRPM_Antenna_1ā€,
ā€œinServiceā€: true,
ā€œchangeTimeā€: 0,
ā€œrfPathDelayNSā€: 0,
ā€œlatitudeā€: 42.321,
ā€œaltitudeā€: 62.125,
ā€œlongitudeā€: 105.781,
ā€œtypeā€: ā€œRPMā€,
ā€œfeedsā€: ,
ā€œpedestalConfigā€: {
ā€œidā€: ā€œ4b25d95d-600e-466f-890f-50e84e292e44ā€,
ā€œicdImplā€: ā€œDE-3002ā€,
ā€œrotorConfigsā€: {
ā€œELā€: {
ā€œidā€: ā€œ804e33c4-d959-425d-becd-fd9c52ded61eā€,
ā€œupperLimitā€: 250.0,
ā€œlowerLimitā€: -90.0,
ā€œinstallationDateā€: null,
ā€œoffsetā€: 5.0,
ā€œstowPositionā€: 25.0,
ā€œrotorPropertiesā€: {}
},

What kind of assertions do you need? To verify that the values are exactly that? Or do you need to verify just that the fields exist and are of a specific data type?

The simplest form is matching the exact values you have, but it wonā€™t work with other responses (since the values will likely change)

const jsonData = pm.response.json();
pm.test('Has correct values', function(){
  pm.expect(jsonData).has.property('name', 'RPM_Antenna_1');
  pm.expect(jsonData).has.property('latitude', 42.321);
  pm.expect(jsonData).has.property('upperLimit', 250.0);
  pm.expect(jsonData).has.property('lowerLimit', -90.0);
});

Using pm.expect this way will verify that your response has the specific properties with the passed in values.

There are ways you can make this dynamic and richer as time goes on, like parameterizing the variable values, or validating the response against a schema. But just give it time for you to understand the pieces and how everything fits together.

Thank you, Allen for the response. However, I am encountering the error shown below when running the assertion. Any further help you can provide would be appreciated.

FAIL
Has correct values | AssertionError: expected { Object (@type, id, ā€¦) } to have property ā€˜upperLimitā€™

Does the response have the upperLimit property?

Yes, the response is what I have in my first question above.

@Straub2148 is this a public facing API that we can use or an internal project? if itā€™s public facing can we get the URL and the relevant GET/POST information please?

{
   "@type":"AntennaConfig",
   "id":ā€œ9e22f537-0f68-4caf-9d98-445555578d27ā€,
   "name":ā€œRPM_Antenna_1ā€,
   "inService":true,
   "changeTime":0,
   "rfPathDelayNS":0,
   "latitude":42.321,
   "altitude":62.125,
   "longitude":105.781,
   "type":"RPM",
   "feeds":,
   "pedestalConfig":{
      "id":ā€œ4b25d95d-600e-466f-890f-50e84e292e44ā€,
      "icdImpl":ā€œDE-3002ā€,
      "rotorConfigs":{
         "EL":{
            "id":ā€œ804e33c4-d959-425d-becd-fd9c52ded61eā€,
            "upperLimit":250.0,
            "lowerLimit":-90.0,
            "installationDate":null,
            "offset":5.0,
            "stowPosition":25.0,
            "rotorProperties":{
               
            }
         },

@allenheltondev I could be wrong, but doesnā€™t the expect have to follow the JSON path down?

e.g.

pm.expect(jsonData).has.property('pedestalConfig.rotorConfigs.EL.upperLimit', 250.0);

or

pm.expect(jsonData. pedestalConfig.rotorConfigs.EL.upperLimit).to.equal(250.0);

@Straub2148

pm.expect(jsonData).has.property('pedestalConfig.rotorConfigs.EL.upperLimit', 250.0);

or

pm.expect(jsonData. pedestalConfig.rotorConfigs.EL.upperLimit).to.equal(250.0);

The error for the second case is less clear than in the first, if the property is missing.
In the first case, it will tell you the property canā€™t be found.
In the latter case, it will tell you that undef does not equal 250.0

But basically, yah, you need to add the complete json node path.

const jsonData = pm.response.json();
pm.test('Has correct values', function(){
  pm.expect(jsonData).has.property('name', 'RPM_Antenna_1');
  pm.expect(jsonData).has.property('latitude', 42.321);
  pm.expect(jsonData).has.property('pedestalConfig.EL.upperLimit', 250.0);
  pm.expect(jsonData).has.property('pedestalConfig.EL.lowerLimit', -90.0);
});

@Straub2148 try the above and let us know :slight_smile:

Yup, thanks guys, using dot notation to trace down the nested objects is absolutely the way to go.

I couldnā€™t tell the objects were nested the way it was pasted in the OP. :man_shrugging:

This is an internal project.

Encountered the error shown below when trying the new assertion.

FAIL
Has correct values | AssertionError: expected { Object (@type, id, ā€¦) } to have property ā€˜pedestalConfig.EL.upperLimitā€™

@Straub2148 please try what I have put in my post:

pm.expect(jsonData).has.property('pedestalConfig.rotorConfigs.EL.upperLimit', 250.0);

That error clearly shows you are missing .rotorConfigs

Wendy was confirming my post and is missing this path

Let me know

Hey @Straub2148 ,

@JBird20 is right on this.

To help you in the future for working with json, if youā€™re trying to get to a specific value, you need to add a dot every time you see a curly brace ā€œ{ā€


So for your specific example, the way to get to upperLimit is:

pedestalConfig.rotorConfigs.EL.upperLimit

Iā€™m still encountering the error when using this:

const jsonData = pm.response.json();

pm.test(ā€˜Has correct valuesā€™, function(){

pm.expect(jsonData).has.property(ā€˜pedestalConfig.rotorConfigs.EL.upperLimitā€™, 250.0);

});

Error: Has correct values | AssertionError: expected { Object (@type, id, ā€¦) } to have property ā€˜pedestalConfig.rotorConfigs.EL.upperLimitā€™