should.have.property 验证来自对象的键值对

should.have.property to verify key-value pair from object

我想检查对象上是否存在键值对。我使用 should.js 语法:cy.gey(selector).should('have.property', 'Compasny Feature: ', ['open space ']); 但我收到此错误 Error: AssertionError: expected { Object (Company Feature: , Name: ) } to have property 'Company Feature: ' of [ 'open space ' ], but got [ 'open space ' ] 我完全不知道哪个部分的值不匹配。或者这只是 should.js?

的错误
// debug output
{
    "Company Feature: ": [
        "open space "
    ],
    "Name: ": [
        "John, Amazon "
    ]
}
// test.ts
cy.gey(selector).should('have.property', 'Compasny Feature: ', ['open space ']);



// html
<div class="container">
    <span>
        <span class="title">Company Feature: </span>
        <span class="text">open space </span>
    </span>
    <span>
        <span class="title">Name: </span>
        <span class="text">John, Amazon </span>
    </span>
</div>

问题只是您引用了数组的不同实例。您可以通过引用数组的相同实例来解决此问题,如下所示:

const testFeature = [
        "open space "
    ];
const TestData = {
    "Company Feature: ": testFeature,
    "Name: ": [
        "John, Amazon "
    ]
}
// test.ts
cy.gey(selector).should('have.property', 'Company Feature: ', testFeature);

或者您可以在链中使用 .deep 并让它根据结构进行检查,请参阅文档:https://www.chaijs.com/api/bdd/#method_property

// test.ts
cy.gey(selector).should('have.deep.property', 'Company Feature: ', testFeature);