data.json
[
{
"id": "trololo_id",
"email": "nasir1985@hotmail.com",
"roles": ["admin", "member"],
"apiKey": ["c0ca4bf0-687a-4eed-a96d-4589bb0f7347"],
"profile": {
"dob": "1992-09-10",
"name": "Rachel Sheppard",
"about": "There are no rules of architecture for a castle in the clouds.;Gilbert K. Chesterton;imagination",
"address": "11 Seagate",
"company": "Verbus",
"location": {
"lat": -79.066,
"lon": -85.69855
}
},
"username": "rachel92",
"createdAt": "2011-01-26T22:11:43.564Z",
"updatedAt": "2011-01-26T22:11:43.564Z"
}
]
Request
Method: POST
URL: https://reqres.in/api/users
Body:
{
"email": "{{email}}",
"roles": ["{{roles}}"],
"profile": {
"name": "{{name}}",
"address": "{{address}}",
"company": "{{company}}"
}
}
Pre-req:
pm.collectionVariables.set("name", data["profile"]["name"]);
pm.collectionVariables.set("address", data["profile"]["address"]);
pm.collectionVariables.set("company", data["profile"]["company"]);
Tests:
pm.test("Status code is 201", function () {
pm.response.to.have.status(201);
});
pm.test("Verify data", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.email).to.eql(data['email']);
pm.expect(jsonData.profile).to.eql(data['profile']);
pm.expect(jsonData.profile.address).to.eql(data['profile']['address']);
pm.expect(jsonData.profile.company).to.eql(data['profile']['company']);
});
Response
{
"email": "nasir1985@hotmail.com",
"roles": [
"admin,member"
],
"profile": {
"name": "Rachel Sheppard",
"address": "11 Seagate",
"company": "Verbus"
},
"id": "209",
"createdAt": "2023-09-17T09:23:11.682Z"
}
Why is there an assertion error and how can I fix it?
Images:
The issue is on the prerequest script (Cannot read properties of undefined (reading "name")
), so the issue is on this line
pm.collectionVariables.set("name", data["profile"]["name"]);
which means that data["profile"]
is undefined. Looking at the mapping of data.json
, the correct syntax should be data[0]["profile"]["name"]
, as data
is an array of objects.