unit testingNodejs Express应用程序

我正在为一个控制器编写一个unit testing,它使用mongoose从MongoDB获取用户列表。 已经写成testing成功和错误的条件。 这是正确和充足的?

或者我需要使用supertest或superagent来使用它的路线来testing这个控制器。 但是这更多的是整合testing,对。 在这里,我也运行我所有的中间件。

// /controllers/users.js exports.list = function (req, res) { User.find({}, function(err, users) { if(err) return applib.handleError(res, err); res.json(users); }); }; // test/users.js var chai = require('chai'); var expect = chai.expect; var sinon = require('sinon'); var users = require('../controllers/users'); var mongoose = require('mongoose'); var User = mongoose.model('User'); var applib = require('../applib'); describe('User CRUD operations', function() { it('should list all users', function(done) { var fake = function(query, cb) { cb(null, [{ id: 1, email: 'foo@foobar.com' }]); }; var stub = sinon.stub(User, 'find', fake); var req = {}; var res = { json: function(data) { expect(data[0].id).to.equal(1); expect(data[0].email).to.equal('foo@foobar.com'); done(); } }; users.list(req, res); stub.restore(); }); it('should err on user listing', function(done) { var fake = function(query, cb) { cb(new Error('Error'), null); }; var stubUserFind = sinon.stub(User, 'find', fake); var stubHandleError = sinon.stub(applib, 'handleError'); var req = {}; var res = {}; users.list(req, res); expect(stubHandleError.withArgs(res, new Error('Error')).calledOnce).to.be.true; stubUserFind.restore(); stubHandleError.restore(); done(); }); });