首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Cypress的类型记录中编写API测试?

如何在Cypress的类型记录中编写API测试?
EN

Stack Overflow用户
提问于 2022-10-08 02:32:42
回答 1查看 40关注 0票数 2

因此,我试图为contacts编写一个API测试,这意味着:

  1. 第一次测试将创建一个联系人2。第二次测试将使用get联系人列表3检查创建的联系人。

这就是我在测试文件中所写的:

代码语言:javascript
复制
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);
                })
            })
        })


})

第四种测试似乎不能正常工作,我无法理解原因。

代码语言:javascript
复制
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并在多个测试中使用它。欢迎提出建议。

EN

回答 1

Stack Overflow用户

发布于 2022-10-08 03:15:24

updateContact()之后,您将需要return contactId,类似于getContactList()getContactList()部分。

代码语言:javascript
复制
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);
  });
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73993981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档