所以我正在尝试为联系人 api 编写 API 测试,这意味着:
这是我在测试文件中写的:
import { getContactList, delContact, createContact, updateContact } from "../base/RequestType";
import { HeadersType } from "../base/HeaderType";
import * as url from '../../fixtures/Url.json';
describe('mfax Api end-points', () => {
let contactId
it('Get contacts list', () => {
createContact(url.endpoint.createcontact,HeadersType.contentJSON)
.then(({ status, body }) => {
expect(status).to.eq(200);
expect(body).to.have.property('uuid');
const contactId = body.uuid;
return contactId;
})
.then((contactId) => {
return getContactList(url.endpoint.getcontacts, HeadersType.contentJSON)
.then(({ status, body }) => {
expect(status).to.eq(200);
const { count, rows } = body;
expect(Array.isArray(rows)).eq(true);
const filteredCount = rows.filter(contact => contact.uuid === contactId).length;
expect(filteredCount).to.eq(1);
return contactId;
})
})
.then((contactId) => {
updateContact(
contactId,
{
'name':'newname',
},
HeadersType.contentJSON,
)
.then(({status, body}) => {
expect(status).to.eq(200);
const {
name,
} = body;
expect(name).to.eq("newname");
})
})
.then((contactId) => {
delContact(
contactId,
HeadersType.contentJSON,
)
.then((resp) => {
expect(resp.status).to .eq(204);
})
})
})
})
第四个测试似乎无法正常工作,我无法理解为什么。
Status: 404 - Not Found
Headers: {
"date": "Sat, 08 Oct 2022 02:27:45 GMT",
"content-type": "application/json; charset=utf-8",
"content-length": "64",
"connection": "keep-alive",
"x-powered-by": "Express",
"access-control-allow-origin": "*",
"x-ratelimit-limit": "60",
"x-ratelimit-remaining": "56",
"x-ratelimit-reset": "1665196125",
"cache-control": "no-store",
"pragma": "no-cache",
"x-content-type-options": "nosniff",
"access-control-expose-headers": "Content-Disposition",
"x-server-version": "4.11.0",
"etag": "W/"40-dpCqarGGuD+Tpa/d+EZwPf5l9LU"",
"strict-transport-security": "max-age=31536000; includeSubDomains"
}
Body: {
"error": {
"name": "NotFoundError",
"message": "Contact not found"
}
}
我还尝试搜索一些示例,在这些示例中我可以学习从 API 响应中获取 id 并在多个测试中使用它。欢迎提出建议。
在
updateContact()
之后,您需要 return contactId
,类似于 getContactList()
和 getContactList()
部分。
createContact(...)
...
.then((contactId) => {
return updateContact(
..
).then(({ status, body }) => {
expect(status).to.eq(200);
const { name } = body;
expect(name).to.eq("newname");
return contactId
});
})
.then((contactId) => {
delContact(contactId, HeadersType.contentJSON).then((resp) => {
expect(resp.status).to.eq(204);
});
});