Bre*_*een 0 request mocha.js end-to-end cypress
我的服务器在 Postman 中返回 JSON 响应,其形式如下:
{
"accountType": "CHECKING",
"message": "Your account is overdrawn",
"withdrawalCode": 'SZWW-2000-11-CD'
}
Run Code Online (Sandbox Code Playgroud)
我正在像这样使用 Cypress 发送请求,并尝试断言所有字段。这是我的尝试
cy.request({
method: 'POST',
url: 'http://users/bank-account/withdrawal',
body: {
name: "paul.king@asher-bank.com",
password: "test123"
},
failOnStatusCode:false
}).its('body')
.should('have.property', 'accountType', 'Checking')
.should('have.property', 'message','User with withdrawal ref: \'CCR-001-009-GG\' is overdrawn')
.should('have.property', 'withdrawalCode','SZWW-2000-11-CD')
})
Run Code Online (Sandbox Code Playgroud)
这 3 个属性中只允许第一个属性断言。其他2个是不允许的。我如何断言多个属性。
来自文档.should() - 产量
在大多数情况下,
.should()产生与上一个命令给出的主题相同的主题。Run Code Online (Sandbox Code Playgroud)cy.get('nav') // yields <nav> .should('be.visible') // yields <nav>然而,一些链家改变了话题。在下面的示例中,第二个
.should()生成字符串sans-serif,因为链接器have.css, 'font-family'更改了主题。Run Code Online (Sandbox Code Playgroud)cy.get('nav') // yields <nav> .should('be.visible') // yields <nav> .should('have.css', 'font-family') // yields 'sans-serif' .and('match', /serif/) // yields 'sans-serif'
解决方法之一是使用回调函数:
.should($body => {
expect($body.prop('accountType').to.eq('Checking')
expect($body.prop('message').to.eq('User with withdrawal ref: \'CCR-001-009-GG\' is overdrawn')
expect($body.prop('withdrawalCode').to.eq('SZWW-2000-11-CD')
})
Run Code Online (Sandbox Code Playgroud)
或单独
.should($body => { expect($body.prop('accountType').to.eq('Checking') })
.and($body => { expect($body.prop('message').to.eq('User with withdrawal ref: \'CCR-001-009-GG\' is overdrawn') })
.and($body => { expect($body.prop('withdrawalCode').to.eq('SZWW-2000-11-CD') })
Run Code Online (Sandbox Code Playgroud)