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: