Sunday 31 January 2016

Express Controller Unit Testing Question

Hey everybody,I have a function in my controller that I want to test but I'm not sure how to go about doing it.CONTEXT: My application has an api which goes through a router and currently, the post for /api/object/create is mapped to the objectController.create() function withrouter.post(/api/object/create,objectController.create) My controller looks like this: exports.create = function(req,res){ //User will submit an object for creation with req.body.name and req.body.longName var Objects = models.Object; console.log(req.body); Objects.create( {number:req.body.number, name: req.body.name, longName: req.body.longName}) .then(function(result){ res.send(result); }) .catch(function(err){ errorController.databaseError(err,req,res); }) } Now how do I create unit tests for this? I've followed this tutorial which tells me to use Sinon.spy to observe the res.send() and ensure it gets called but that does not work. http://ift.tt/1UAsjjn have seen a lot of tutorials calling for me to test by having the server running and actually sending POST's to the api, but that seems like End-to-end testing and is not what I'm looking for.Any help would be much appreciated :) Thanks!

Submitted January 31, 2016 at 06:28PM by TLI5

No comments:

Post a Comment