unit testing express route with async callback

被刻印的时光 ゝ 提交于 2019-12-23 20:17:03

问题


I'm writing an app in node.js using express.
I seperated the route definition from express so I will be able to unit test it without mocking out express.
What I did is:

file: usersRoutes.js

 var routes = {
  getAll: function(req,res) {
    var db = req.db; // req.db is initialized through a dedicated middleware function
    var usersCollection = db.get('users');
    usersCollection.find({},{limit:10},function(e,results){
      res.send(results);
    });
  }
};

module.exports = routes;

file: users.js

var express = require('express');
var router = express.Router();
var routes = require('./usersRoutes');

/* GET users listing. */
router.get('/', routes.getAll);

module.exports = router;

and finally in app.js:

var users = require('./routes/users/users.js');
app.use('/users', users);

Now, I want to write a unit test that checks that req.send is being called with the right parameter. I'm having trouble figuring out the correct way because the req.send is invoked asynchronously.
What is the correct way to unit test this function?


回答1:


if you are using mocha it provides the "done" function that you can inject into your callback and it will tell mocha that test is async and should wait until that function is called

it('should do something',function(done){
  reuest.send(function(){
     expect(true).toEqual(true);
     done()
  })
})

or something like that i don't remember is this is 100% the right syntax, but is pretty close this is good for callbacks but if you are using promises on the other hand you should check chai and mocha's promises assertions, are pretty cool




回答2:


I was looking for the answer to your question, was wondering if you ever managed to solve it. My work around was to have the route (middleware) return the 'promise' in the function. Then in my unit test you can do

return middleware(req, res).then(function () {
    expect(res.render).to.have.been.calledWith('whatever');
});


来源:https://stackoverflow.com/questions/27721082/unit-testing-express-route-with-async-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!