在赛普拉斯中,如何在正文中对带有参数的POST API请求进行存根处理? [英] In Cypress, how do I stub a POST API request with parameters in the body?

查看:95
本文介绍了在赛普拉斯中,如何在正文中对带有参数的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"?

推荐答案

一个选项是使用 Mirage.js

https://miragejs.com/docs/comparison-with-other-tools/#cypress -route-mocks

请参阅其教程: https: //miragejs.com/quickstarts/cypress/

这篇关于在赛普拉斯中,如何在正文中对带有参数的POST API请求进行存根处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆