所以,我正在测试一个依赖于事件发射器的组件。为此,我想出了一个使用Promise with Mocha + Chai的解决方案:
它(‘应该用正确的事件过渡’,(完成)=> { const cFSM
万一你有 async helperFunction() 从您的测试中调用...(ES7的一个明确说明 async 关键字,我的意思是)
async helperFunction()
async
鈫鈫确保,你称之为 await helperFunction(whateverParams) (好吧,是的,很自然,一旦你知道......)
await helperFunction(whateverParams)
为了工作(避免“等待是一个保留字”),您的测试函数必须具有外部异步标记:
it('my test', async () => { ...
我遇到了这个问题:
(node:1131004)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(re jection id:1):TypeError:res.json不是函数 (node:1131004)弃用警告:不推荐使用未处理的拒绝承诺。 将来,未处理的承诺拒绝将终止Node.j 使用非零退出代码的进程。
这是我的错,我正在取代 res 对象 then(function(res) ,改变了 res 结果,现在它正在运作。
res
then(function(res)
module.exports.update = function(req, res){ return Services.User.update(req.body) .then(function(res){//issue was here, res overwrite return res.json(res); }, function(error){ return res.json({error:error.message}); }).catch(function () { console.log("Promise Rejected"); });
module.exports.update = function(req, res){ return Services.User.update(req.body) .then(function(result){//res replaced with result return res.json(result); }, function(error){ return res.json({error:error.message}); }).catch(function () { console.log("Promise Rejected"); });
服务代码:
function update(data){ var id = new require('mongodb').ObjectID(data._id); userData = { name:data.name, email:data.email, phone: data.phone }; return collection.findAndModify( {_id:id}, // query [['_id','asc']], // sort order {$set: userData}, // replacement { "new": true } ).then(function(doc) { if(!doc) throw new Error('Record not updated.'); return doc.value; }); } module.exports = { update:update }