更新时间:2022-04-14 03:28:15
使用赛普拉斯5.1,使用新的 route2
命令,模拟GraphQL非常简单请求,例如:
With cypress 5.1, using the new route2
command it is very simple to mock GraphQL requests, for example:
cy.route2('/graphql', (req) => {
if(req.body.includes('operationName')){
req.reply({ fixture: 'mockData.json'});
}
});
我刚刚添加了一个if条件,以评估GraphQL请求的正文是否包含某些字符串作为查询的一部分。
如果是这样,那么我会从固定装置中加载自定义主体进行回复。
I just added an if condition to evaluate if the body of the GraphQL request contains certain string as part of the query. If that is true, then I reply back with a custom body loaded from a fixture.
cy.route2()$ c的文档$ c>:
https://docs.cypress.io/api/命令/route2.html