How to exclude a dynamic value when testing response body with lodash _.isEqual(jsonData,

We are developing API’s and I need the ability to verify the entire payload of certain response bodies with exception of a dynamic “generateDateTime”: “2020-03-09T16:24:50.58”, value. I’m using lodash test snippet below which works perfectly for static response bodies that have no dynamic values… But I need a solution for either verifying the static response body + the dynamic “generateDateTime”: or to somehow exclude the “generateDateTime”: value in the test assertion.

var jsonData = JSON.parse(responseBody);
var _ = require(‘lodash’);
tests[“Correct Response Body is Returned”] = _.isEqual(jsonData,

{

“My Response Body”

}
});

Thanks!!!

Hi Chris,

I just wanted to point out that the more modern way of writing tests in Postman is using the Chai.js assertion library. All code snippets support that now.

Since jsonData is a JavaScript object, I would simply remove the property generateDateTime from the response.

delete jsonData.generateDateTime;

Thank you for the reply! Just for clarification your saying that I should be using the Chai assertions as opposed to lodash? I apologize I’m a bit of a newbie to Postman can you be specific about how and where to use the delete jsonData.generateDateTime; within the lodash test assertion I provided?

OK after some trail and error I this is what worked: Thanks again!! I’ve been doing some reading on Chai “expect” I think it will work well too.

var jsonData = JSON.parse(responseBody);
delete jsonData.generationDateTime;
var _ = require(‘lodash’);
tests[“All Correct JSON is Displayed”] = _.isEqual(jsonData,

{
“srFoo”: 5201,
“srFooName”: “Transaaction”,
“pageId”: 5204,
“pageName”: “Entry”,
"generationDateTime": “2020-03-11T11:29:47.893”,****(Removed property)
“modifiedDate”: “2019-03-15T14:43:38.763”,
“pageButtons”: [
{
“name”: “Next”,
“text”: “Next”,
“isDefault”: true,

You don’t really need lodash, if you ask me.

I would write the test like this:

pm.test("Correct Response Body is Returned", () => {
    const expectedResponse = { "foo": "bar"};
    const response = pm.response.json();
    delete response.generateDateTime;
    pm.expect(response).to.eql(expectedResponse);
});
1 Like

I agree with @vdespa here, for me Lodash isn’t needed and the newer pm.test function makes things more readable, in my opinion. :grin:

Also, for most Lodash functions like isEqual you don’t need to use a require statement to bring them into the sandbox environment. So if you removed the var _ = require(‘lodash’); from your code, it would still work. :grin: