- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有没有办法让 Cypress 捕获任何未使用 cy.intercept 处理和 stub 的请求。我希望 Cypress 返回一个有用的错误,以突出显示发出未 stub 请求的实例。目前它只是让这些请求通过,这没有什么帮助。
如果有一种方法可以保证处理程序是链中的最后一个处理程序,那么它的架构方式将允许使用一个包罗万象的处理程序,但看起来没有任何方法可以做到这一点。
最佳答案
这在技术上是可行的,这里有一个演示测试。
我的模式基于这样的前提:拦截是在最后定义的基础上处理的(忽略中间件类型),因此包罗万象的内容是在所有其他内容之前定义的。
catch-all 使用 *
作为匹配器,它将捕获绝对所有内容,但仅捕获那些尚未被另一个拦截捕获的内容。
it('catching unstubbed requests', () => {
const unstubbedRequests = []
cy.intercept('*', (req) => unstubbedRequests.push(req))
// these requests I have stubbed already
cy.intercept('https://jsonplaceholder.typicode.com/todos/1', {stub:1}).as('stub1')
cy.intercept('https://jsonplaceholder.typicode.com/posts/1', {stub:2}).as('stub2')
// make some calls on the app window, simulating real app calls
cy.window().then(win => {
win.fetch('https://jsonplaceholder.typicode.com/todos/1') // should stub
win.fetch('https://jsonplaceholder.typicode.com/posts/1') // should stub
win.fetch('https://jsonplaceholder.typicode.com/users/1') // should not stub
win.fetch('https://jsonplaceholder.typicode.com/photos/1') // should not stub
})
cy.wait('@stub1')
cy.wait('@stub2')
cy.wrap({}).should(() => {
expect(unstubbedRequests.length).to.eq(2)
expect(unstubbedRequests[0].url).to.eq('https://jsonplaceholder.typicode.com/users/1')
expect(unstubbedRequests[1].url).to.eq('https://jsonplaceholder.typicode.com/photos/1')
})
})
注释
如果您想将其应用到现实世界的应用程序中,此策略并非万无一失。
上面我使用了 expect(unstubbedRequests.length).to.eq(2)
因为我知道会有两个未捕获的请求,并且 .should()
code> 如果发送请求有一些延迟,命令将重试(此模拟中没有任何延迟)。
为了解决这个问题,在您的测试中,您需要在检查 unstubbedRequests
数组之前提供某种等待方式
cy.wait(some-maximum-time)
或
但是如果等待时间不够长,这些等待方法可能会不稳定。
这是试图在测试中证明否定的结果。
理想情况下,您希望最终测试“知道”将发送的所有请求,但作为临时测试开发工具,此技术可能很有用。
关于javascript - 使用 cy.intercept 时捕获未处理的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76582264/
我在等待异步功能完成时苦苦挣扎。特别是,我发现这两种方法在测试继续之前等待异步函数完成,但不知道其中的区别(如果有区别的话):。我的目标是在实际测试开始之前等待bepreEach()块中的两个异步函数
我在等待异步功能完成时苦苦挣扎。特别是,我发现这两种方法在测试继续之前等待异步函数完成,但不知道其中的区别(如果有区别的话):。我的目标是在实际测试开始之前等待bepreEach()块中的两个异步函数
我在等待异步功能完成时苦苦挣扎。特别是,我发现这两种方法在测试继续之前等待异步函数完成,但不知道其中的区别(如果有区别的话):。我的目标是在实际测试开始之前,在beforeEach()块中等待两个Ja
我正在尝试在我的代码中尽可能多地使用 data-cy。 必须一直编写 cy.get('[data-cy=name]') 有点乏味。 是否可以创建一个默认情况下首先尝试查找数据的自定义命令。 所以如果我
我有一种情况,我想在拦截中生成 JWT token 并将其返回到它的主体中。 问题在于,在该 token 的有效载荷中,应该使用另一个拦截的值。 这意味着我只能在拦截期间生成 token 。 cons
findByText来自 Cypress 测试库 https://testing-library.com/docs/cypress-testing-library/intro 和 contains C
我们在本地测试的应用程序有一个运行在 上的前端和后端。本地主机:4200 和 127.0.0.1:8000 , 分别。 打电话时cy.visit('/somepage')我们希望它有一个不同的 bas
我有一个编辑器页面。当我添加任何内容并单击“保存”按钮时,我的 URL 将更改,在 URL 中添加一个随机 ID。我想在每次单击“保存按钮”时检查我的 ID 是否在更改。 我将 URL 结果保存在变量
我正在尝试在每次测试之前使用 Cypress 进行编程登录。我的 token 保存在 localStorage 中。如果我同步更改它,即简单地做 localStorage.setItem不涉及cy.r
Cypress.io 中的 cy.readFile 和 cy.fixture 有什么区别?我们应该在什么情况下使用 cy.readFile 和 cy.fixture ? cy.readFile('me
看起来它们是相同的。 cy.get("a").find("b") 和 cy.get("a b") 之间的输出有什么不同吗? (其中a和b是一些选择器,例如div和span,或者。 someClass
我想使用 cypress-audit 进行灯塔测试,但在完成他们在 https://www.npmjs.com/package/cypress-audit 上所说的一切之后它不起作用。我可以在 cyp
我试图在请求正文上断言以确保正确的新测试卡作为订单的一部分被传递。 it("User clicks confirm & pay button to complete order", () => {
我想在 CDialog 的 OnInitDialog 期间获取 cx 和 cy。 我可以用下面的代码做到这一点: myDialog::OnInitDialog() { CRect rcWindow
在使用 Cypress 一段时间后,我注意到 cy.wrap() 有一个奇怪的行为。虽然在钩子(Hook)之前将字符串包装在内部,但表示字符串在所有进一步测试中都可用,但对象仅在第一次测试中可用,而在
我需要一些关于如何处理以下情况的帮助。有一个模式,当关闭时发送 AJAX 请求,如果响应成功,则重绘 Datatables 表。我需要在触发 draw.dt 事件后测试表格的内容。 我获得了对表的引用
我正在尝试匹配网址的一部分 http://www.mywebsite.com/get-stuff在 cypress 中,并且无法弄清楚如何编写正则表达式匹配。 我试过: cy.contains('ht
在改用哈希路由器之前,我经常执行 cy.url 命令,以确保整个应用程序中的链接导航到正确的 URL 地址。现在我们使用哈希路由 cy.url 不再生成字符串,而是生成函数。有什么想法可以解决这个问题
我试图依次选择两个按钮,一个“删除”按钮,然后选择"is"弹出确认窗口,方法如下: cy.get('.btn-danger').last().click(); cy.get('.btn-primary
我已经在测试项目中安装了 ESlint,它开始显示我需要解决的一些错误 错误之一是在 cy.request('someURL'); 错误是 cy 未定义 所以我在这样的文件顶部添加了一个导入语句 im
我是一名优秀的程序员,十分优秀!