簡體   English   中英

Cypress 攔截 - 如何在響應上鏈接多個斷言

[英]Cypress intercept - how to chain multiple assertions on a response

我剛開始使用新的攔截方法,有一個基本問題,想知道如何在一個測試中鏈接下面的兩個斷言。

cy.intercept('GET', '/states').as('states');
cy.reload(true);
// cy.wait('@states').its('response.statusCode').should('eq',200)
cy.wait('@states').its('response.body').should('have.length', 50)

兩個斷言單獨工作。

.its('response.statusCode')傳下來的主題是statusCode屬性的值,所以你需要再次訪問response來測試這兩個條件

使用閉包為兩個斷言提供response

cy.wait('@states')
  .its('response')
  .then(response => {
    cy.wrap(response).its('statusCode').should('eq', 200)     
    cy.wrap(response).its('body').should('have.length', 50)
  })

使用回調模式

cy.wait('@states')
  .its('response')
  .should(response => expect(response.statusCode).to.eq(200))   
  .should(response => expect(response.body.length).to.eq(50))

重讀別名

cy.wait('@states')                                   // wait for the alias
  .its('response.statusCode').should('eq', 200)

cy.get('@states')                                    // 2nd time use get()
  .its('response.body').should('have.length', 50)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM