Varun Sukheja
Varun Sukheja

Reputation: 6516

Mocha Chai - Test multiple properties of object

I want to check if my response object contains mentioned properties using chai should assertion.

Below is my code snippet:

chai.request(app)
        .post('/api/signup')
        .send(
            data
        )
        .then(function (response) {
            response.should.have.status(200);
            response.body.should.have.property('active', 'mobileNumber', 'countryCode', 'username', 'email', 'id', 'createdAt', 'updatedAt', 'location');
            done();
        })
        .catch(function (error) {
            done(error);
        })

But I am getting below error: enter image description here

Upvotes: 10

Views: 9573

Answers (2)

Kyle Burriss
Kyle Burriss

Reputation: 751

If you're exclusively utilising Chai (in Postman, for example), the following string of functions will successfully aid you:

response.body.to.include.all.keys("active", "mobileNumber");

The full list of API's (including an example of the above) can be found here.

Upvotes: 3

Varun Sukheja
Varun Sukheja

Reputation: 6516

Found how to achieve it using mocha chai-should,

Function name is .should.have.keys() Just pass the properties to this function and it will check they should be present in the object you are testing.

bellow is the code

response.body.should.have.keys('active', 'mobileNumber', 'countryCode', 'username', 'email', 'id', 'organisationId', 'createdAt', 'updatedAt', 'location');

Upvotes: 17

Related Questions