赛普拉斯:使用三种不同的响应对同一路线进行存根响应 [英] Cypress: Stub response for same route with three different responses

查看:79
本文介绍了赛普拉斯:使用三种不同的响应对同一路线进行存根响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中有一个端点。我们在每个请求中使用params中的不同操作命中相同的api。

I have a single endpoint in the application. We hit the same api for each request with different action in the params.

URL

/application/api

示例请求有效负载1:

{
  "action":"CARD_TRANSACTION_HISTORY",
  "data":{"date_from":"2018-12-01","date_to":"2018-12-31","total":5},
  "meta":{}
}

示例请求有效负载2:

{
  "action":"CARD_BALANCE",
  "data":{"date_from":"2018-12-01","date_to":"2018-12-31","total":5},
  "meta":{}
}

示例请求有效负载3:

{
  "action":"CURRENCY_RATES",
  "data":{"date_from":"2018-12-01","date_to":"2018-12-31","total":5},
  "meta":{}
}

上述请求中的操作针对不同请求的更改。

the action in above request changes for different requests.

当加载仪表板页面时,我们会触发3 并发使用不同操作的AJAX POST请求。

When the dashboard page is loaded, we trigger 3 concurrent AJAX POST requests with different actions.

cypress的问题是你只能为一个路由指定一个响应,而处理这个的其他方法是make sequential requests(我们不能这样做)

Problem with cypress is you can only specify one response for a route, and other way to handle this is make sequential requests (which we can't do)

即使我们将响应写为函数,它也只被调用一次。

Even if we write response as a function it gets called only once.

任何关于我们如何在有效载荷的基础上模拟数据的想法?

Any ideas on how we can mock data on the basis of payload?

推荐答案

我做了一个肮脏的工作,我做了'喜欢它,但我没有选择。

I did one dirty work around that worked, I didn't like it but I am out of options.

我只是将所有回复合并到同一个回复中。

I simply combined all the responses in the same response.

我的模拟回复

{
  balance: {..},
  transactionHistory: {..},
  currencyRates: {..}
}

每个响应处理程序只是处理它感兴趣的部分,如果其中一个响应是数组,我们需要将它更改为一个对象。

The each response handler simply processes part it is interested in, if one of the response is array, we'll need to change it to an object.

我会留意一个更好的解决方法。

I'll be on a lookout for a better work around.

这篇关于赛普拉斯:使用三种不同的响应对同一路线进行存根响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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