lundi 31 août 2015

Unable to do unit testing for an API in sequelize using Mocha and Chai

Hi I am new to unit testing. I am currently working on Mocha,Chai and Sequelize to make a TDD unit test for an API. But while running the unit test using Mocha, I get the following error:

 msg: 'TypeError: Cannot read property \'firstName\' of undefined' } to not exist

The unit test I have written for my API is as follows:

describe('createUser', function () {
it('should create a User', function (done) {
    var email_Id = "xyz@gmail.com";
    var firstName = "xyx";
    var values = JSON.stringify({
        body: {
            email_Id: email_Id,
            firstName: firstName
        }
    });
    user.createUser(values, function (err, response) {
        expect(response).should.be.an(object);
        expect(err).to.be.null;
    });
    done();
   });
});

My API is as follows:

createUser: function (req, res) {
    var param = req.body;
    var firstName = param.firstName;
    var email_Id = param.email_Id;
    db.sequelize.sync().then(function () {
        user.create(param).then(function (response) {
            // return a Success Response in JSON format

        }).catch(function (err) {
           // return an Error Response in JSON format
        });
    });
  }
}

Can Somebody help me as to why am I getting this error?

Aucun commentaire:

Enregistrer un commentaire