在使用Cy.Intercep的同一端点与Cypress JavaScript一起使用CY.INTERCEP时,如何在夹具文件夹中制作多个.json文件

发布于 2025-02-09 03:01:47 字数 1182 浏览 1 评论 0原文

在这一点上,我将API端点与柏树截距固定。 它运行良好,但是我感觉可以更有效地使用它。

目前,我有以下测试:

 cy.intercept('GET', '**/Classifications', { fixture: 'stubClassifications/Classifications5.json' })
        cy.get('div.dropdown-menu.show').find('a.dropdown-item').should('have.length', 5)

正在工作,并检查文件分类。

“在此处输入图像说明”

classification5.json: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
  }
]

在以下测试中,我正在固定相同的api-endpoint,但与只有1个分类,即分类1.json。

如您所见,我在固定图中制作了几个.JSON文件,每次我断言的结果,这看起来不太好且干净。

在使用CY.Intercept的同一终点时,我如何防止在地图固定装置中制作多个.json文件。

At this point I am stubbing API endpoints with Cypress Intercept.
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.:

enter image description here

Classifications5.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.

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.

How can I prevent making multiple .json files in the map fixtures when using the cy.intercept for the same end-point.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余生再见 2025-02-16 03:01:47

似乎您在说classifation1.jsonclassification5.json的较小版本?

如果是这样,您只需要classification5.json文件。可以从中得出较小的文件。

cy.fixture('Classifications5.json').then(fiveItems => 
  const oneItem = fiveItems.slice(0,1)
  cy.intercept('GET', '**/Classifications', oneItem)

在两项测试

cy.fixture('Classifications5.json').then(fiveItems => 
  const twoItems = fiveItems.slice(0,2)
  cy.intercept('GET', '**/Classifications', twoItems)

生成固定装置

中,如果更改的唯一变化是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)

It seems like you are saying that Classifications1.json is a smaller version of Classifications5.json?

If so you only need the Classifications5.json file. 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.

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文