javascript - How to do unit test in dependent modules (node.js)? -
i have application 2 modules (book , user). book model looks this:
var mongoose = require('mongoose'), schema = mongoose.schema; var bookmodel = new schema({ name: string, author: string, description: string, _user: {type: schema.types.objectid, ref: 'user'} }); module.exports = mongoose.model('book', bookmodel);
and user model:
var mongoose = require('mongoose'), bcrypt = require('bcryptjs'), schema = mongoose.schema; var usermodel = new schema({ name: string, username: string, password: string, }); module.exports = mongoose.model('user', usermodel);
i want unit test in post method (inserting book in db) condition of not allowing empty name. have in post method:
var post = function (req, res) { var booknew = new book(req.body); // paylod user's token var payload = tokenmanager.getpayload(req.headers); if (req.body._user) delete req.body._user; if (!req.body.name) { res.status(400); res.send("name required"); } else if (payload == null || payload == undefined) { res.status(400); res.send("token error"); } else { // store user id booknew.set('_user', payload.id); booknew.save(); res.status(201); res.send("book saved"); } };
as can see, payload
token (created when user logged in). because payload contains user id and, then, insert in _user
(property in book model).
the problem if unit test in order verify if book’s property (name
) filled, don’t have user retrieve payload. therefore, payload undefined , book unit test not successful in case. have suggestion on should do? looks have create user in order test book modules… not sure if suitable solution.
Comments
Post a Comment