赛普拉斯请求:正文中的空数组
Cypress request : empty array in body
我在使用 Cypress 测试 API 时遇到了一些麻烦。 (我使用的是 2.1.0 版)
我正在向我的端点发送一个请求,并想验证当我发送一个空数组作为参数时它是如何反应的。问题是,不知何故,赛普拉斯必须解析我给他的正文,并删除空数组。
我的代码如下:
cy.request({
method: 'PUT',
url,
form: true,
body: {
name: 'Name',
subjects: []
}
})
.then((response) => {
expect(response.body).to.have.property('subjects');
const { subjects } = response.body;
expect(subjects.length).to.eq(0);
});
// API receives only the parameter name, and no subjects
当我发送一个空主题数组时,端点将删除所有关联的主题,并且 return 具有一个空主题数组的对象。它正常工作,我使用的软件也正常工作。
当 Cypress 发送此请求时,端点 不会 接收参数 subjects。这对我来说是完全不同的事情:在这种情况下我不应该触及主题。
有没有办法通过 Cypress 避免这种情况 "rewriting" 并在我写的时候发送正文?
设置form: false
时测试有效。
it.only('PUTs a request', () => {
const url = 'http://localhost:3000/mythings/2'
cy.request({
method: 'PUT',
url: url,
form: false,
body: {
name: 'Name',
subjects: []
}
})
.then((response) => {
expect(response.body).to.have.property('subjects');
const {
subjects
} = response.body;
expect(subjects.length).to.eq(0);
});
})
我用 json-server 设置了一个本地休息服务器来检查行为。
如果我尝试使用 form: true
放置一个非空数组
cy.request({
method: 'PUT',
url: url,
form: true,
body: {
name: 'Name',
subjects: ['x']
}
})
在测试 运行 之后查看 db.json,我看到项目索引迁移到键中,
"mythings": [
{
"name": "Name",
"subjects[0]": "x",
"id": 2
}
],
所以也许 form
仅表示简单属性。
更改为 form: false
给出了正确的数组
{
"mythings": [
{
"name": "Name",
"subjects": ['x'],
"id": 2
}
],
}
然后可以通过发布一个空数组来清空它。
我在使用 Cypress 测试 API 时遇到了一些麻烦。 (我使用的是 2.1.0 版)
我正在向我的端点发送一个请求,并想验证当我发送一个空数组作为参数时它是如何反应的。问题是,不知何故,赛普拉斯必须解析我给他的正文,并删除空数组。
我的代码如下:
cy.request({
method: 'PUT',
url,
form: true,
body: {
name: 'Name',
subjects: []
}
})
.then((response) => {
expect(response.body).to.have.property('subjects');
const { subjects } = response.body;
expect(subjects.length).to.eq(0);
});
// API receives only the parameter name, and no subjects
当我发送一个空主题数组时,端点将删除所有关联的主题,并且 return 具有一个空主题数组的对象。它正常工作,我使用的软件也正常工作。
当 Cypress 发送此请求时,端点 不会 接收参数 subjects。这对我来说是完全不同的事情:在这种情况下我不应该触及主题。
有没有办法通过 Cypress 避免这种情况 "rewriting" 并在我写的时候发送正文?
设置form: false
时测试有效。
it.only('PUTs a request', () => {
const url = 'http://localhost:3000/mythings/2'
cy.request({
method: 'PUT',
url: url,
form: false,
body: {
name: 'Name',
subjects: []
}
})
.then((response) => {
expect(response.body).to.have.property('subjects');
const {
subjects
} = response.body;
expect(subjects.length).to.eq(0);
});
})
我用 json-server 设置了一个本地休息服务器来检查行为。
如果我尝试使用 form: true
cy.request({
method: 'PUT',
url: url,
form: true,
body: {
name: 'Name',
subjects: ['x']
}
})
在测试 运行 之后查看 db.json,我看到项目索引迁移到键中,
"mythings": [
{
"name": "Name",
"subjects[0]": "x",
"id": 2
}
],
所以也许 form
仅表示简单属性。
更改为 form: false
给出了正确的数组
{
"mythings": [
{
"name": "Name",
"subjects": ['x'],
"id": 2
}
],
}
然后可以通过发布一个空数组来清空它。