在 Cypress 中,如何存根带有正文参数的 POST API 请求?

And*_*ell 11 stub cypress

我正在用 Cypress 编写一个端到端测试,我想存根我的应用程序发出的网络请求。具体来说,我想删除多个在正文中有参数的 POST 请求,并根据这些参数更改我的模拟响应。

我想做类似的事情

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",
})
Run Code Online (Sandbox Code Playgroud)

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

我可以通过将函数传递给 的onRequest参数来完成此操作cy.route吗?如果是这样,我会从那个告诉赛普拉斯“这条路线实际上不处理这个请求”的函数返回什么?

小智 0

cy.route({
  method: "POST",
  url: "/todos/add"
  body: {
    "urgency": 1,
  },
  response: "fixture:add2.json",
})
Run Code Online (Sandbox Code Playgroud)