在摩卡柴测试中如何检查一个数组的所有元素是否都有子属性?

问题描述 投票:0回答:1

我正在尝试测试一个来自服务器的响应。假设响应是一个数组,其元素有一组属性,对应的值是具有其他属性的对象。

例如类似这样的东西。

{
 "array": [
        {
            "porp1": {
                "subprop11": "a",
                "subprop12": [1,2]
            },
            "prop2": "c",
        },
        {
            "porp1": {
                "subprop11": "h",
                "subprop12": [3,2]
            },
            "prop2": "a",
        }
    ]
}

我想检查数组的所有元素是否有属性和子属性。对于第一级属性,我使用了以下方法 chai-things:

const chai = require('chai');
chai.use(require('chai-things'));
....
res.body['array'].should.all.have.property('prop1');
res.body['array'].should.all.have.property("prop2");

如何检查元素是否有子属性?"subprop11" 和`"subprop12"?

node.js testing mocha chai
1个回答
0
投票

像这样的东西似乎可以工作

res.body['array'].should.all.have.nested.property('prop1.subprop11');
© www.soinside.com 2019 - 2024. All rights reserved.