两条路径的Minimatch模式,其中一条是另一条的前缀 [英] Minimatch pattern for two paths where one is prefix of the other

查看:68
本文介绍了两条路径的Minimatch模式,其中一条是另一条的前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写与Cypress的集成测试,但在minimatch模式上遇到了麻烦.

我有两个需要存根的端点./users/1 /users/1/profile .

我尝试使用 cy.intercept()模拟这两个端点的方式如下.对于第一个网址/users/1 ,我尝试了 cy.intercept('GET','/users/1',{}).

对于第二个网址/users/1/profile ,我尝试了 cy.intercept('GET','/users/1/profile',{}).

问题在于第一个模式拦截两次.

我可以在这方面寻求帮助吗?谢谢.

解决方案

当我第一次使用 cy.intercept 时,我也遇到了这个问题.解决方案是将 RouteMatcher 对象传递给该方法.特别是,您将需要使用下图中的最后一个方法签名:

RouteMatcher 对象中,可以指定 path 属性.这是 path 属性的描述:

本质上,使用 RouteMatcher 对象的 path 属性与给定的字符串完全匹配,而 url 参数在第一种方法签名和第二种方法签名会与给定的字符串进行子字符串匹配.

所以您想要的是:

  cy.intercept({方法:'GET',路径:'/users/1'},{身体: {}})cy.intercept({方法:'GET',路径:'/users/1/profile'},{身体: {}}) 

我认为,赛普拉斯在 cy.route cy.intercept 方法之间的这种细微变化是很奇怪的,并且在首次使用时有些意外./p>

I am writing an integration test with cypress and having trouble with minimatch pattern.

I have two endpoints that I need to stub. /users/1 and /users/1/profile.

The way I am trying to mock these two endpoints with cy.intercept() is the following. For the first url, /users/1, I tried cy.intercept('GET', '/users/1', {}).

For the secton url , /users/1/profile, I tried cy.intercept('GET', '/users/1/profile', {}).

The problem is that the first pattern intercepts twice.

Can I get some help on this?? Thanks.

解决方案

I, too, fell into this problem when first using cy.intercept. The solution is to pass in a RouteMatcher object to the method. In particular, you'll want to use the last method signature from the image below:

In the RouteMatcher object, you can specify a path property. Here's the description of the path property:

In essence, using the path property of the RouteMatcher object does an exact match against the given string, whereas the url parameter in the 1st and 2nd method signatures does a substring match against the given string.

So what you'll want is:

cy.intercept(
    {method: 'GET', path: '/users/1'},
    {body: {}}
)

cy.intercept(
    {method: 'GET', path: '/users/1/profile'},
    {body: {}}
)

In my opinion, this slight change from Cypress between the cy.route and cy.intercept methods was weird and a bit unexpected on the first run-through.

这篇关于两条路径的Minimatch模式,其中一条是另一条的前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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