在赛普拉斯中,如何在正文中对带有参数的POST API请求进行存根处理?

问题描述:

我正在用赛普拉斯编写端到端测试,我想对应用程序发出的网络请求进行存根。具体来说,我想在主体中包含参数的多个POST请求存根,然后根据这些参数更改模拟响应。

I am writing an end-to-end test with Cypress and I would like to stub the network requests which my application makes. Specifically, I would like to stub out multiple POST requests which have parameters in the body and to change my simulated response based on those parameters.

我想做类似的事情

cy.route({
  method: "POST",
  url: "/todos/add"
  params: {
    "urgency": 3,
    "stakeholder_id": "SKH001"
  },
  response: "fixture:add1.json",
})

cy.route({
  method: "POST",
  url: "/todos/add"
  params: {
    "urgency": 1,
  },
  response: "fixture:add2.json",
})

但在读完
https://docs.cypress.io/guides/guides/network-requests.html https://docs.cypress.io/api/commands/route.html#Arguments ,我没有看到支持的方法来检查存根请求中的参数。

But after reading through https://docs.cypress.io/guides/guides/network-requests.html and https://docs.cypress.io/api/commands/route.html#Arguments, I do not see a supported way of checking the arguments in the request being stubbed.

我可以通过将函数传递给 cy.route的 onRequest 参数来完成此操作吗? ?如果是这样,我将从告诉cypress此路由实际上不处理此请求的函数返回什么?

Can I accomplish this by passing a function to the onRequest parameter of cy.route? If so, what would I return from that function which tells cypress "this route actually does not handle this request"?