首页 > 解决方案 > 赛普拉斯没有检测来自 cy.route() 的路由

问题描述

我已经准备好后端了。但我希望我的前端在测试时只使用我们可以在 cypress 中创建的模拟服务器。我的测试是

        cy.server()
        cy.route({
        method:'POST',
        url:'/dashboard/v1/public/login',
        status:404,
        response:{
                "error": true,
                "message": "User with above credentials does not exists"
            }
        })

        cy.get('[data-testid="loging-page-email-input')
        .type('test@gmail.com')


        cy.get('[data-testid="loging-page-password-input')
        .type('test123')


        cy.get('[data-testid="loging-page-button-clicked')
            .type('{enter}')

        cy.get('[data-testid="loging-page-auth-error-msg') // Here it should grab alert box

但问题是,如果我们想物理登录网站,相同的凭据是正确的。因此,如果 cypress 能够登录,那么我们可以看到它没有获取我们提供的数据

cy.route({
        method:'POST',
        url:'/dashboard/v1/public/login',
        status:404,
        response:{
                "error": true,
                "message": "User with above credentials does not exists"
            }
        })

相反,它正在进行 api 调用并且能够登录。


知道出了什么问题吗?更多的是我的基本网址,http://localhost:3000但我的完整登录 api 网址是y.x.in/dashboard/v1/public/login


更新 我现在也给了身体路线

            cy.server()
            cy.route({
                method:'POST',
                url:'/dashboard/v1/public/login',
                body:{
                    email:"s@gmail.com",
                    password:"sss"
                },
                status:404,
                response:{
                    "error": true,
                    "message": "User with above credentials does not exists"
                }
            })


在此处输入图像描述 我刚刚检查了单击登录按钮后,cypress 没有捕获登录 api 调用

标签: reactjscypress

解决方案


尝试这种方法,请注意method,urlresponse不在options参数范围内。

cy.server()
cy.route({
    method:'POST',
    url:'/dashboard/v1/public/login',
    response: {
        "error": true,
        "message": "User with above credentials does not exists"
    },
    status:404,
    options: {
        body:{
            email:"s@gmail.com",
            password:"sss"
        }
    }
})

推荐阅读