I'm trying to add Unit Tests to my Node.js project, and have being able to successfuly do so, until a while that I feel like I've hit a wall.
I'm not sure how should I do the tests on my controller, since it doesn't return anything but after resolving a couple of promises.
My simplified code looks like this
exports.userCreate = function (request, reply, next) {
var response;
Utils.validateSchema(data)
.then(_userUtils.userExistsKo)
.then(_userUtils.appAuthorized)
.then(_userUtils.userInsert)
.then(function () {
response = Utils.createResponseData(UserResponses.user_signed_up_ok);
return reply(response).code(response.result.statusCode);
})
.fail(function (err) {
response = Errors.createGeneralError(err);
return reply(response).code(err.statusCode);
});
};
Right now I'm trying to do something like this in my Jasmine file
it('should call the required utils functions', function () {
expect(UtilsMock.validateSchema).toHaveBeenCalled();
expect(userUtils.userExistsKo).toHaveBeenCalled();
})
Where UtilsMock.validateSchema and userUtils.userExists have been set up as spies and configured with rewire.
I'm using Node.js with Hapi framework, and Jasmine for the tests, not that it matters, I'm looking for a more generic answer regarding testing implementations.
Long story short, what would be the best way to test the code above?
Aucun commentaire:
Enregistrer un commentaire