【问题标题】:How to validate POST xhr如何验证 POST xhr
【发布时间】:2018-11-06 14:08:14
【问题描述】:

我如何验证这个 xhr 帖子是否被中止(在这种情况下,它应该是积极的场景并且测试应该通过?

【问题讨论】:

    标签: typescript cypress


    【解决方案1】:

    好的,我刚刚解决了。

    describe('Register', function() {
      it('Step 0', function() {
        cy.visit('localhost:4200')
      })
      it('Email validation', function() {
        cy.server();
        cy.route('POST', '**sign_in').as('signin');
    
        cy.get('[data-cy="signup-input"]').type('test@test.pl')
        cy.get('[data-cy=input-password]').type('test123456{enter}')
        cy.get('[data-cy="email-error1"]').should('be.visible')
    
        cy.wait('@signin').its('status').should('equal', 401)
    
    
      })
    })

    【讨论】:

      【解决方案2】:

      也可以使用 chai expect 断言来完成,如下所示:

      cy.wait('@signin').then((xhr) => {
          expect(xhr.status).to.equal(401);
      });
      

      您可以阅读更多关于它的信息in cypress documentation for cy.wait()。他们的XHR Assertions examples github project 中也有大量示例。

      【讨论】:

        【解决方案3】:

        这是解决它的新方法:

          cy.intercept(
            {
            method:'GET',
            url:'/api/channels/e5932cce
            }).as('loginLoaded')
         
          cy.get("@loginLoaded").then((xhr) => {
            cy.wait('@loginLoaded').its('response.statusCode').should('eq', 401)
            })
        

        【讨论】:

          猜你喜欢
          • 2016-08-27
          • 2016-09-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-05-26
          • 1970-01-01
          • 2011-02-03
          • 2012-10-02
          相关资源
          最近更新 更多