0

hey guys im new in postman

this is my response

{
    "status": "ok",
    "statusCode": "0000",
    "message": {
        "text": "",
        "type": ""
    },
    "errors": [],
    "data": {
        "docs": [
            {
                "name": "admin",
                "description": "نقش admin کل سیستم. با دسترسی به همه‌ی بخش ها (System generated)",
                "code": "T11AsR",
                "permissions": [
                    "authenticated",
                    "manage_users",
                    "view_user_logs",
                    "manage_role",
                    "map_role_to_user",
                    "managing_custom_page",
                    "managing_FAQ_page",
                    "managing_jobs_page",
                    "managing_about_us_page",
                    "send_sms",
                    "read_sms",
                    "send_email",
                    "read_email",
                    "manage_system_settings",
                    "manage_profile",
                    "manage_account",
                    "manage_images",
                    "manage_videos",
                    "verify_email",
                    "manage_country_province_city",
                    "manage_resumes",
                    "get_user_report"
                ],
                "protected": true,
                "userType": "admin",
                "_id": "60b2607d6a286901dffe61fa"
            },
            {
                "name": "public",
                "description": "دسترسی های این نقش برای همه کاربران ثبت نام شده و احراز هویت شده مجاز است.",
                "code": "fh1xqE",
                "permissions": [
                    "authenticated",
                    "manage_profile",
                    "manage_account",
                    "verify_email"
                ],
                "protected": true,
                "userType": "client",
                "_id": "60b2607f6a286901dffe61fb"
            }
        ],
        "totalDocs": 2,
        "limit": 100,
        "totalPages": 1,
        "page": 1,
        "pagingCounter": 1,
        "hasPrevPage": false,
        "hasNextPage": false,
        "prevPage": null,
        "nextPage": null
    }
}

i wanna test that this code “T11A21” dont be in the response

i dont know what is the code my test is check this id shouldent be in this response

in fact in this test i should test that this id shouldnt be in response because i removed it in past and in this request now i should check it completly removed

i dont write a test for this api i thing i should use " for " and check all length of the response but im not sure

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • if you only need to check if a substring is included in a larger string, you can use `String.prototype.includes()` https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes – GrafiCode Aug 26 '21 at 16:35
  • if you actually want to traverse the JSON representation of that string and check `data.docs.code`, you should first parse the string using `JSON.parse()` https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse – GrafiCode Aug 26 '21 at 16:37
  • i just want to check this value dont be in response but i dont know how – Amir Marezloo Aug 26 '21 at 18:06

3 Answers3

0

Use Array.prototype.some() to test if any of the codes are equal to the one you're checking. If this returns false, then it's not in the response.

let no_T11A21 = !response.data.docs.some(doc => doc.code == "T11A21")
Barmar
  • 741,623
  • 53
  • 500
  • 612
0
itemList = pm.response.json().data.docs.filter((item)=>item.code==="T11A21")

pm.expect(itemList).to.be.empty

this will be better as it gives the information on where the id was discovered and it helps you to debug . Else with "some" you have to manually debug to see which object had the code.

PDHide
  • 18,113
  • 2
  • 31
  • 46
0

You need to first traverse to the JSON representation till "code": "T11A21"

pm.expect(response.data.doc.code).to.not.eql("T11A21");
Prarna Dhar
  • 11
  • 1
  • 3