[Cypress] Test XHR Failure Conditions with Cypress

Testing your application’s behavior when an XHR call results in an error can be difficult. The use of stubs for XHR calls makes it easy for us to setup failure scenarios and ensure that our front-end responds the way we expect. In this lesson, we’ll stub a 500 response for a form submission and verify that our application responds appropriately.

 

复制代码
    it('should show an error message for a failed from subission', function () {
        const newTodo = "Test";
        cy.server();
        cy.route({
            method: 'POST',
            url: '/api/todos',
            status: 500,
            response: {}
        }).as('save');

        cy.seedAndVisit();

        cy.get('.new-todo')
            .type(newTodo)
            .type('{enter}');

        cy.wait('@save');

        cy.get('.todo-list li').should('have.length', 4);
        cy.get('.error').should('be.visible');
    });
复制代码

posted @   Zhentiw  阅读(315)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2017-05-26 [RxJS] Convert RxJS Subjects to Observables
2017-05-26 [HTTP] Understand 2xx HTTP Status Code Responses
2017-05-26 [React] Cleanly Map Over A Stateless Functional Component with a Higher Order Component
2016-05-26 [PWA] Enable Push Notification in your web app
2016-05-26 [RxJS] Combination operator: withLatestFrom
2016-05-26 [RxJS] Combination operator: zip
2016-05-26 [RxJS] Combination operator: combineLatest
点击右上角即可分享
微信分享提示