We have a simple Postman test task here that ensures the product is available by keeping the current-stock value above 0. Although, the emphasis will be rather on the JavaScript syntax rule of using key names with hyphens.
Our response example looks like this:
{
"id": 5,
"name": "coconut",
"type": "fruit",
"price": 9.10
"current-stock": 20
}
The initial test script:
let response = pm.response.json()
pm.test("Stock should't be 0, it must be above 0", () => {
pm.expect(response.current-stock).to.be.above(0)
});
When we try to implement a key name that contains a hyphen, like this .current-stock, we will receive a Postman error saying that “stock is not defined.” This is because key names that contain a hyphen can’t be used in a function like single-word key names.
Here is the correct way of using it:
let response = pm.response.json()
pm.test("Stock should be above 0", () => {
pm.expect(response['current-stock']).to.be.above(0)
});