1

I'm a newbie in using postman as testing web rest API. Can you help me in this simple error? The error says: There was an error in evaluating the test script: ReferenceError: jsonData is not defined.

Thanks in advance!

pm.test("User ID is 1 -> " +jsonData[0].userId, function () {
var jsonData = pm.response.json();
pm.expect(jsonData[0].userId).to.eql(1);
});

enter image description here

Lyra Opena
  • 111
  • 2
  • 14

1 Answers1

1

Are you using the standalone version of postman, or the chrome extension? If you're using the chrome extension, you'll find that pm is undefined, as stated here

Though perhaps not exactly had in mind, if you are working with the chrome extension, you can defined "testing logic" in a slightly different way via the Tests tab:

enter image description here

The Testing tab allows you to write tests that are automatically run across the response data that returns after your click "Send".

This basically lets you write and execute javascript in Postmans own testing context, allowing you to define some fairly elaborate test logic to verify that your response data returns the expected results:

var contentTypeHeaderExists = responseHeaders.hasOwnProperty("Content-Type");

tests["Has Content-Type"] = contentTypeHeaderExists;

if (contentTypeHeaderExists) {
    tests["Content-Type is application/json"] = 
      responseHeaders["Content-Type"].has("application/json");
}

See this blog post for more information on using the Testing tab - hope this helps you!

Dacre Denny
  • 29,664
  • 5
  • 45
  • 65