简体   繁体   English

在cypress中将同一端点存根两次

[英]Stub the same endpoint twice in cypress

I want to stub the same API endpoint twice, so the second call returns different response than the first one. 我想对同一个API端点进行两次存根,因此第二个调用返回的响应不同于第一个调用。 Here is snippet how I imagine this would work: 这是我如何想象的片段:

cy.route('POST', 'access-tokens', '@loginFailResponse', {status: 401}).as('loginFail')
cy.route('POST', 'access-tokens', '@loginSuccessResponse').as('loginSuccess')

The first time I try to login it denies access, I change form inputs and then it should let me in. 我第一次尝试登录时拒绝访问,我更改了表单输入,然后让我进入。

I tried to wrap second cy.route(...) definition as callback function to first output but cypress denies calling cy.anything in promises. 我试图将第二个cy.route(...)定义包装为第一个输出的回调函数,但cypress拒绝在promise中调用cy.anything Like in example below 像下面的例子

cy.route('POST', 'access-tokens', '@loginFailResponse', {status: 401, onResponse: () => {
      cy.fixture('login_screen/login_success_response.json').as('loginSuccessResponse')
      cy.route('POST', 'access-tokens', '@loginSuccessResponse').as('loginSuccess')}
}}).as('loginFail')

here is my test case: 这是我的测试用例:

cy.route('POST', 'access-tokens', '@loginFailResponse', {status: 401}).as('loginFail')
cy.route('POST', 'access-tokens', '@loginSuccessResponse').as('loginSuccess')
cy.get("form input[type='email']").type("bad@email.com")
cy.get("form input[type='password']").type("Bad password")
// this should fail
cy.get("form").submit()
cy.get("form input[type='password']").type("g00d@password.com")
// this should let me in
cy.get("form").submit()

Everytime you define .route('VERB', '/endpoint', ...) it overrides your previous definition. 每次定义.route('VERB', '/endpoint', ...)它都会覆盖以前的定义。 The simplest solution would be to override this endpoint after you finish your first call 最简单的解决方案是在您完成第一个呼叫后覆盖此端点

This test will work for you, Filip. Filip,这项测试将为您服务。

cy.route('POST', 'access-tokens', '@loginFailResponse', {status: 401}).as('loginFail')
cy.get("form input[type='email']").type("bad@email.com")
cy.get("form input[type='password']").type("Bad password")
// this should fail
cy.get("form").submit()
cy.route('POST', 'access-tokens', '@loginSuccessResponse').as('loginSuccess')
cy.get("form input[type='password']").type("g00d@password.com")
// this should let me in
cy.get("form").submit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM