Create tests to check the field and sub value

I wish to check if the resourceTypeName is ClearingPalette and respective actionType and resourceTypeDescription matches the same data. The permissionsids keeps changing.

I have a response body as below:

{
    "status": "success",
    "data": [
        {
            "permissionId": 1,
            "resourceTypeName": "ClearingCandidateSet",
            "actionType": "DELETE",
            "resourceTypeDescription": "Clearing Candidate Set"
        },
        {
            "permissionId": 2,
            "resourceTypeName": "ValidationRule",
            "actionType": "CREATE",
            "resourceTypeDescription": "Validation Rule"
        },
        {
            "permissionId": 3,
            "resourceTypeName": "ClearingCandidate",
            "actionType": "DOWNLOAD",
            "resourceTypeDescription": "Clearing Candidate"
        },
        {
            "permissionId": 4,
            "resourceTypeName": "ClearingPalette",
            "actionType": "CREATE",
            "resourceTypeDescription": "Clearing Palette"
        }
]}

Please, help

Your response has a data object which is an array of objects.

You will need to find the object with the resourceTypeName which you can then assert on the actionType and resourceTypeDescription.

Something like…

const response = pm.response.json();

pm.test('ClearingPalette, actionType and resourceTypeDescription match', () => {
    pm.expect(response.data).to.be.an('array').that.is.not.empty;
    let search = response.data.find(obj => obj.resourceTypeName === "ClearingPalette");
    pm.expect(search).to.not.be.undefined;
    pm.expect(search.actionType).to.eql("CREATE");
    pm.expect(search.resourceTypeDescription).to.eql("Clearing Palette");
});

Please remember to make the test fail by changing the assertions to ensure that you are not getting a false positive. (For example, change CREATE to Create).

You might want to consider splitting the tests like following.

const response = pm.response.json();

let search = response.data.find(obj => obj.resourceTypeName === "ClearingPalette");

pm.test('ClearingPalette: exists in response', () => {
    pm.expect(search).to.not.be.undefined;
});

pm.test('ClearingPalette: actionType = Create', () => {
    pm.expect(search.actionType).to.eql("CREATE");
});

pm.test('ClearingPalette: resourceTypeDescription = Clearing Palette', () => {
    pm.expect(search.resourceTypeDescription).to.eql("Clearing Palette");
});

This way, you get feedback on all three tests. In the first example. If the actionType assertion fails, then the test will fail immediately and you won’t get feedback on the final assertion. You might have an issue with the description and not know about it until you fix the actionType test. Which is why its sometimes useful to split the tests so you get feedback on all of the assertions.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.