简体   繁体   中英

How to check if all the elements of an array have subproperties in mocha-chai testing?

I am trying to test a response from a server. Suppose the response is an array whose elements have a set of properties and the corresponding values are objects having other properties.

For example something like:

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

I want check all the elements of the arrays to have the properties and sub-properties. For the first level properties I managed to do that using 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");

How can I check the elements to have the subproperties "subprop11" and `"subprop12"?

Something like this seems to work

res.body['array'].should.all.have.nested.property('prop1.subprop11');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM