Context
I m actually developping a little API using Restify and Mongoose and I want to work with unit tests.
Actually, my project looks like this, using CoffeeScript :
Controller class :
mongoose = require 'mongoose'
Task = require '../models/Task'
InternalServerError = require('restify').errors.InternalServerError
# Task controller class
#
# @author Some Developer
# @version v1.0.0
class TaskController
# Get a Task object from its ID field
#
# @param {Object} req Restify request object
# @param {Object} res Restify response object
getById: (req, res)->
if req.params.id
Task.findOne({keyId: req.params.id}, '-_id -__v')
.then((result)=>
if !result
res.send 200, {}
else
res.send 200, result
, (err)=>
req.log.error({err: 500}, err)
res.send(new InternalServerError('Internal server error'))
)
else
res.send(new BadRequestError('The :id field hasn\'t been found in the request'))
module.exports = TaskController
A boot class that instantiate and uses this controller like :
controller = new TaskController()
@server.get({path: '/tasks', version: '1.0.0'}, controller.getAll)
@server.get({path: '/tasks/:id', version: '1.0.0'}, controller.getById)
@server.post({path: '/tasks', version: '1.0.0'}, controller.add)
@server.put({path: '/tasks/:id', version: '1.0.0'}, controller.edit)
@server.del({path: '/tasks/:id', version: '1.0.0'}, controller.delete)
Problem
1/ I have read many article concerning the way of unit testing Mongoose object. The fact is that every article I m looking really deals with that in the database, like counting the number of objects after a save() call on a mongoose object. I dont want to really use my data in database. It seems that I can use mocks to avoid this right ?
2/ How can I unit test the getById function exposed above ? I mean, there's not return statement, except the response code (okay, some unit test there) but my probleme is, how to test mongoose behaviour inside of this function ?
Aucun commentaire:
Enregistrer un commentaire