gpt4 book ai didi

javascript - 如何通过 Cypress 对下面的正文请求进行断言。谢谢

转载 作者:行者123 更新时间:2023-12-04 13:06:38 30 4
gpt4 key购买 nike

我需要使用 Cypress 测试我在以下 JSON 中拥有的属性。我想我在接触每个属性并验证它们时遇到了问题。

describe('POST method', () => {
it('get a list of faceted aggregations', () => {

cy.request({
method: 'POST',
url:"host+endpoints",

headers:{
"Authorization":"xxxxxxx",
"content-type": "application/json"
},

body: {
"search": {
"auctionType": [
"basic_auction"

],
"statuses": [
"ongoing"
],
"sites": [
"autobid.de"
],
"urls": [
"yyyyyyy"
],
"ahIds": [
53825-62-2014918
],
"biddingPlatforms": [
"aaaaaaa"
],
"winningPlatforms": [
"aaaaa"
],
"losingPlatforms": [
"aaaaaa"
],
"hasBids": true,
"hasVat": true,
"query": "string",
"range": [
{
"auctionEndDatetime": {
"min": "string",
"max": "string"
}
},
{
"auctionEndDatetime": {
"min": "string",
"max": "string"
}
},
{
"auctionStartDatetime": {
"min": "string",
"max": "string"
}
},
{
"createdDatetime": {
"min": "string",
"max": "string"
}
},
{
"updatedDatetime": {
"min": "string",
"max": "string"
}
},
{
"price": {
"min": "string",
"max": "string"
}
}
],
"exists": [
"string"
],
"filters": {
"vehicles": [
{
"brand": "string",
"modelIds": 0,
"model": "string"
}


}
}

我的回复正文:

{"data": {
"count": 252655,
"aggregations": {
"filters.bodyTypes": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Station wagon",
"doc_count": 45778
},
{
"key": "SUV",
"doc_count": 30188
},
{
"key": "Hatchback",
"doc_count": 25059
},
{
"key": "Saloon",
"doc_count": 13636
},
{
"key": "Van",
"doc_count": 11266
},
{
"key": "MPV",
"doc_count": 10158
},
{
"key": "Estate",
"doc_count": 9700
},
{
"key": "Estate, Number of doors 5",
"doc_count": 6800
},
{
"key": "Berline avec hayon",
"doc_count": 6276
},
{
"key": "Saloon, Number of doors 5",
"doc_count": 5238
},
{
"key": "berline hayon - VP",
"doc_count": 3298
},
{
"key": "All-terrain vehicle, Number of doors 5, All-wheel drive",
"doc_count": 2783
},
{
"key": "Stationwagon",
"doc_count": 2727
},
{
"key": "Saloon, Number of doors 4",
"doc_count": 2466
},
{
"key": "",
"doc_count": 2270
},
{
"key": "Van / minibus, Number of doors 5",
"doc_count": 2058
},
{
"key": "Other",
"doc_count": 1989
},
{
"key": "Berlina",
"doc_count": 1901
},
{
"key": "H",
"doc_count": 1867
},
{
"key": "Crossover",
"doc_count": 1860
},
{
"key": "Panel van",
"doc_count": 1833
},
{
"key": "HA -BERLINA CON PORTON",
"doc_count": 1796
},
{
"key": "SUV VP - VP",
"doc_count": 1771
},
{
"key": "Estate, Number of doors 5, All-wheel drive",
"doc_count": 1760
},
{
"key": "Higher Mid-Range Cars",
"doc_count": 1748
},
{
"key": "Station Wagon",
"doc_count": 1673
},
{
"key": "Monovolume",
"doc_count": 1603
},
{
"key": "Saloon, Number of doors 3",
"doc_count": 1547
},
{
"key": "W",
"doc_count": 1513
},
{
"key": "Mid-range Cars",
"doc_count": 1492
},
{
"key": "All-terrain vehicle, Number of doors 5",
"doc_count": 1453
},
{
"key": "Wagon",
"doc_count": 1210
},
{
"key": "break - VP",
"doc_count": 1201
},
{
"key": "ES -FAMILIAR",
"doc_count": 1158
},
{
"key": "SW",
"doc_count": 1064
},
{
"key": "Berline 5 portes",
"doc_count": 1017
},
{
"key": "Personal car",
"doc_count": 994
},
{
"key": "Sedan",
"doc_count": 994
},
{
"key": "SUV Medium",
"doc_count": 949
},
{
"key": "SUV4x2",
"doc_count": 946
},
{
"key": "berline VU - VS",
"doc_count": 938
},
{
"key": "F",
"doc_count": 896
},
{
"key": "Convertible / Roadster, Number of doors 2",
"doc_count": 894
},
{
"key": "CI",
"doc_count": 867
},
{
"key": "Stationwagen",
"doc_count": 835
},
{
"key": "Monovolume 4x4",
"doc_count": 805
},
{
"key": "mini MS - VP",
"doc_count": 802
},
{
"key": "Light duty truck, Number of doors 4",
"doc_count": 738
},
{
"key": "BREAK",
"doc_count": 717
},
{
"key": "Mixed car",
"doc_count": 702
},
{
"key": "VU1",
"doc_count": 677
},
{
"key": "M",
"doc_count": 673
},
{
"key": "Coupe",
"doc_count": 665
},
{
"key": "MONO-M",
"doc_count": 655
},
{
"key": "Saloon, Number of doors 4, All-wheel drive",
"doc_count": 634
},
{
"key": "Mini MPV",
"doc_count": 588
},
{
"key": "crossover - VP",
"doc_count": 584
},
{
"key": "OD -TODOTERRENO",
"doc_count": 536
},
{
"key": "CV -FURGON DERIVADO DE TURISMO",
"doc_count": 530
},
{
"key": "Compact cars",
"doc_count": 529
},
{
"key": "4x4",
"doc_count": 527
},
{
"key": "fourgonnette - VU",
"doc_count": 524
},
{
"key": "Estate, Number of doors 4",
"doc_count": 511
},
{
"key": "Break VP",
"doc_count": 504
},
{
"key": "Coupé",
"doc_count": 503
},
{
"key": "Kombi",
"doc_count": 490
},
{
"key": "Saloon, Number of doors 5, All-wheel drive",
"doc_count": 481
},
{
"key": "Fourgonnette",
"doc_count": 454
},
{
"key": "Berline VU 5 portes",
"doc_count": 452
},
{
"key": "KOM",
"doc_count": 437
},
{
"key": "S",
"doc_count": 427
},
{
"key": "Sports car / Coupé, Number of doors 2",
"doc_count": 426
},
{
"key": "coupe - VP",
"doc_count": 426
},
{
"key": "SA -SEDAN",
"doc_count": 414
},
{
"key": "MM -MONOVOLUMEN COMPACTO",
"doc_count": 413
},
{
"key": "Light duty truck",
"doc_count": 390
},
{
"key": "Fourgon lourd",
"doc_count": 384
},
{
"key": "SUV 4x4",
"doc_count": 382
},
{
"key": "Furgon largo",
"doc_count": 356
},
{
"key": "MONO-S",
"doc_count": 352
},
{
"key": "businessclass Cars",
"doc_count": 352
},
{
"key": "berline - VP",
"doc_count": 349
},
{
"key": "Mini monospace",
"doc_count": 332
},
{
"key": "Minivan",
"doc_count": 326
},
{
"key": "fourgon tôle - VU",
"doc_count": 319
},
{
"key": "Van / minibus, Number of doors 4",
"doc_count": 315
},
{
"key": "Fourgon leger",
"doc_count": 307
},
{
"key": "Kasten",
"doc_count": 284
},
{
"key": "Convertible",
"doc_count": 266
},
{
"key": "MPV (multi-purpose vehicle)",
"doc_count": 252
}
]
}
}
}
}

我根据 Mihi 的评论进行了更改。

describe('POST method', () => {
it('get a list of faceted aggregations', () => {
cy.request({
method: 'POST',
url:"host + endpoint",

headers:{
"Authorization":"xxxx",
"content-type": "application/json"
},

body: {
"search": {
"auctionType": [
"basic_auction"
],
"statuses": [
"ongoing"
],
"sites": [
"autobid.de"
],
"urls": [
"https://autobid.de?action=car&show=details&id=2014918&L=1"
]
}
},

failOnStatusCode: false


})
.then((res) => {

return response.body.data.aggregations['filters.bodyTypes'].buckets
})
.then(bodyTypeObjects => {

expect(bodyTypeObjects.length).to.eq(90)

const bodyTypeCounts = bodyTypeObjects.reduce((acc, bodyTypeObject) => {
acc[bodyTypeObject.key] = bodyTypeObject.doc_count
return acc
}, {})
return bodyTypeCounts
})
.then(bodyTypeCounts => {
expect(res.status).to.eq(200)

expect(bodyTypeCounts).to.have.property('Station Wagon')
expect(bodyTypeCounts['Station Wagon']).to.be.gt(1000)

运行后报错如下:

request POST 200 https:xxxx
2 then function(){}
ReferenceError
response is not defined
cypress/integration/specs/API-Testing/auctionAggregations.spec.js:41:15
39 | .then((res) => {
40 |
> 41 | return response.body.data.aggregations['filters.bodyTypes'].buckets
| ^
42 | })
43 | .then(bodyTypeObjects => {
44 |

最佳答案

对我来说,测试前最好把大json转化一下。

响应有这样的嵌套

{
data: {
count: 252655,
aggregations: {
"filters.bodyTypes": {
doc_count_error_upper_bound: 0,
sum_other_doc_count: 0,
buckets: [
{
key: "Station wagon",
doc_count: 45778,
},

但这使得测试代码更难理解。

首先使用这个函数提取聚合

(response) => {
return response.body.data.aggregations['filters.bodyTypes'].buckets
}

然后您可以检查 bodyTypes 的数量

cy.request({
method: 'POST',
...
})
.then(response => {
return response.body.data.aggregations['filters.bodyTypes'].buckets
})
.then(bodyTypeObjects => {
expect(bodyTypeObjects.length).to.eq(90)
})

要检查单个项目,您需要解构 bodyTypeObjects,例如

const match = Cypress._.filter(bodyTypeObjects, Cypress._.matches({key: 'Station Wagon'}))
expect(match).to.have.length(1)

但是为了使测试代码更简单,从这个形状做另一个转换

[{key: 'Station wagon', doc_count: 45778}, {key: 'SUV', doc_count: 30188}, ...] 

这个形状

{ Station wagon: 45778, SUV: 30188, ... }

然后测试变得简单很多

完整测试

cy.request({
method: 'POST',
...
})
.then(response => {
return response.body.data.aggregations['filters.bodyTypes'].buckets
})
.then(bodyTypeObjects => {

expect(bodyTypeObjects.length).to.eq(90)

const bodyTypeCounts = bodyTypeObjects.reduce((acc, bodyTypeObject) => {
acc[bodyTypeObject.key] = bodyTypeObject.doc_count
return acc
}, {})
return bodyTypeCounts
})
.then(bodyTypeCounts => {

expect(bodyTypeCounts).to.have.property('Station Wagon')
expect(bodyTypeCounts['Station Wagon']).to.be.gt(1000)

// OR

cy.wrap(bodyTypeCounts)
.should('have.property', 'Station Wagon', 1673)

})

关于javascript - 如何通过 Cypress 对下面的正文请求进行断言。谢谢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69100235/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com