Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocha Chai - Test multiple properties of object

Tags:

mocha.js

chai

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

like image 593
Varun Sukheja Avatar asked Mar 14 '18 14:03

Varun Sukheja


People also ask

What is Chai's assertion style to compare the contents of objects?

Important Concept: By default, all assertions in Chai are performing a strict equality comparison. Thus, asserting that an array of objects has a member object will cause those two objects to be compared strictly. Adding in the deep flag signals to the assertion to instead use deep equality for the comparison.

What is chai expect?

expect , should = chai. should(); The expect interface provides a function as a starting point for chaining your language assertions. It works on node. js and in all browsers.

What is chai in JavaScript?

Chai is an assertion library that is mostly used alongside Mocha. It can be used both as a BDD / TDD assertion library for NodeJS and can be paired with any JavaScript testing framework. It has several interfaces that a developer can choose from and looks much like writing tests in English sentences.


1 Answers

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');
like image 72
Varun Sukheja Avatar answered Sep 28 '22 03:09

Varun Sukheja