Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postman test throws "TypeError: Cannot read property 'get' of undefined"

How do I make it work?

Goal: call a variable inside test script

Test script:

pm.test("shipment registration not allowed", function () {
    var jsonData = pm.response.json();
 pm.expect(jsonData.results[0].errors.title).to.eql(pm.errors.get("shipmentRegistrationNotAllowed"));
});

The error:

Error: TypeError: Cannot read property 'get' of undefined
like image 788
Sunguresat Avatar asked Apr 10 '18 10:04

Sunguresat


1 Answers

I'm not sure where you have found pm.errors.get as a function that you can use but I don't believe it is something that is within Postman.

All the sandbox functions can be found here https://www.getpostman.com/docs/v6/postman/scripts/postman_sandbox_api_reference

If you are just looking to assert that jsonData.results[0].errors.title equals a string value, you could just do this:

pm.test("shipment registration not allowed", () => {
    var jsonData = pm.response.json()
    pm.expect(jsonData.results[0].errors.title).to.eql(pm.globals.get("my_error_value"))
})

If you set a global variable you can reference in the test like this:

enter image description here

like image 175
Danny Dainton Avatar answered Nov 11 '22 09:11

Danny Dainton