如何testing包含asynchronous函数的nodejs导出

我不知道如何testing一个nodejs的导出函数。 考虑下面的代码:

exports.create_expense = (req, res, next) -> User = database.db_model 'user' req.body.parsed_dt = Date.parse(req.body.date) req.body.amount = parseInt(req.body.amount) User.update {_id: req.api_session.id}, {$push: {expenses: req.body}}, (err, numberAffected, raw) -> if err? res.send 500 else res.send 200 

User是这里的mongoose对象。 我想写一个testing(使用摩卡)来testing这个function(在我的testing中,我将调用create_expense ),但由于User.update是asynchronous的,我不能只是调用create_expense而不通过某种forms的Promise? 我知道我可以使用supertest,但也testing了我不想在这里做的路线。 有没有什么办法来testing这个任何npm这里有用?

User.update你应该打电话。 后来在testing中,你应该叫做done

编辑:

对不起,我的CoffeeScript提前。

在callback到User.update结束时再调用。

你的testing应该看起来像这样:

 describe '#create_response', () -> response = false req.body.date = new Date req.body.amount = 123 req.api_session.id = 'asd' res.send = (code) -> response = code it 'should return 500 on invalid request', (done) -> create_expense req, res, () -> assert.equal response, 500 done 
Interesting Posts