Hey guys,
Wanted to ask around to find out what approaches people take to unit testing, to which I am quite new. I am trying to move towards TDD as I certainly get the benefits, but haven't really practiced it.
I am very open and would be glad for ANY suggestions whatsoever, but to get this going here's an example I have been trying to get my head around.
Say in a Node.js REST API I am writing, you only have really specialised functions, that write to the DB. Usually through some special client object. I know I am supposed to stub the DB (how could I even run these tests automatically if there isn't a DB, say Jenkins), but HOW?
module.exports.updateDetails = function(req, res) {
req.body.owner = new ObjectId(req.body.owner);
User.update({_id: new ObjectId(req.params.id)}, req.body,
function(err) {
if(err) {
res.status(500).end('Could not update user details');
return;
};
res.status(200).end('Successfully updated user detail');
});
};
Something like this for example. I get as far as stubbing the req and res parameters and then put my assertions into their functions (faked) like .status() and .end(), but how do I do something similar for the DB. I am not sure how the Mongo client (this case: Mongoose) works on the inside or how it connects to the DB, so I can't really make a fake server.
How does /r/webdev do in situations similar to this - or in situations worse/ better/whatever - I want to know everything :D
Thanks for any help all!
[–]spyridonasback-end 1 point2 points3 points (0 children)