在使用赛普拉斯测试我的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
当我发送空的主题数组时,端点将删除所有关联的主题,并返回带有空主题的对象。它正在按预期方式运行,并且我正在使用的软件正在按预期方式运行。
赛普拉斯发送该请求时,端点不会收到参数subject。对我来说,这是非常不同的事情:在这种情况下,我不应该碰触主题。
有没有一种方法可以避免赛普拉斯的这种“重写”并在我编写正文时将其发送出去?
设置时该测试有效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设置了一个本地rest服务器来检查行为。
如果我尝试使用以下命令放置非空数组 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
}
],
}
然后可以通过发布一个空数组将其清空。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句