[英]How to prevent making multiple .json files in the fixture folder while using cy.intercep for the same endpoint with Cypress Javascript
At this point I am stubbing API endpoints with Cypress Intercept.在这一点上,我正在使用 Cypress Intercept 对 API 端点进行存根。 It is working great, but I've the feeling I could use it more efficient.它工作得很好,但我觉得我可以更有效地使用它。
Currently I have the following test:目前我有以下测试:
cy.intercept('GET', '**/Classifications', { fixture: 'stubClassifications/Classifications5.json' })
cy.get('div.dropdown-menu.show').find('a.dropdown-item').should('have.length', 5)
It's working, and checks the file Classifications5.json where 5 classifications are available.:它正在工作,并检查文件 Classifications5.json,其中有 5 个分类可用。:
Classifications5.json:分类5.json:
[
{
"id": "6a75b703-8af4-4734-8d3f-c259d36b7a5e",
"name": "1STUBTest EEO",
"hasChildren": false
},
{
"id": "6a75b703-8af4-4734-8d3f-c259d36b7a5e",
"name": "2STUBTest EEO",
"hasChildren": false
},
{
"id": "6a75b703-8af4-4734-8d3f-c259d36b7a5e",
"name": "3STUBTest EEO",
"hasChildren": false
},
{
"id": "6a75b703-8af4-4734-8d3f-c259d36b7a5e",
"name": "4STUBTest EEO",
"hasChildren": false
},
{
"id": "6a75b703-8af4-4734-8d3f-c259d36b7a5e",
"name": "5STUBTest EEO",
"hasChildren": false
}
]
In a following test I am stubbing the same API-endpoint but with just 1 classification, namely Classifications1.json.在接下来的测试中,我对相同的 API 端点进行了存根,但只有 1 个分类,即 Classifications1.json。
As you can understand I made several .json file in the fixture map for each result that I am asserting and this doesn't look very nice and clean.正如您所理解的,我在夹具映射中为我断言的每个结果创建了几个 .json 文件,这看起来不太好和干净。
How can I prevent making multiple .json files in the map fixtures when using the cy.intercept for the same end-point.将 cy.intercept 用于同一端点时,如何防止在地图装置中创建多个 .json 文件。
It seems like you are saying that Classifications1.json
is a smaller version of Classifications5.json
?您似乎是在说Classifications1.json
是Classifications5.json
的较小版本?
If so you only need the Classifications5.json
file.如果是这样,您只需要Classifications5.json
文件。 Smaller files can be derived from it.可以从中派生出较小的文件。
cy.fixture('Classifications5.json').then(fiveItems =>
const oneItem = fiveItems.slice(0,1)
cy.intercept('GET', '**/Classifications', oneItem)
In a two-item test在两项测试中
cy.fixture('Classifications5.json').then(fiveItems =>
const twoItems = fiveItems.slice(0,2)
cy.intercept('GET', '**/Classifications', twoItems)
Generating the fixture生成夹具
If the only thing that changes is the name
property, you could generate the fixture on the fly.如果唯一改变的是name
属性,您可以动态生成夹具。
const generateFixture = (numItems) => {
const fixture = [...Array(numItems).keys()].map(key => {
return {
id: "6a75b703-8af4-4734-8d3f-c259d36b7a5e",
name: `${key}STUBTest EEO`,
hasChildren: false
}
})
return fixture
}
...
const twoItems = generateFixture(2)
cy.intercept('GET', '**/Classifications', twoItems)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.