2015-08-26 105 views
1

我有一些简单的基于Promise的代码,尽管我尝试了最好的时间(> 2000ms)。帮帮我?节点/摩卡测试超时

export function listCurrentUserPermissions(req, res, next) { 
    return UserPermission.findAll({ 
    where: { accountId: req.user.tenant() } 
    }).catch((error) => { 
    console.log('-----------------------------------'); 
    console.log(error); 
    console.log.bind(console); 
    console.log('-----------------------------------'); 
    }).then((permissions) => { 
    return res.json({ userPermission: permissions.map(serializeUserPermission) }); 
    }, next); 

而且测试:

describe('GET /api/v0/permissions',() => { 
    it('shows the current users permissions',() => { 
     return api.listCurrentUserPermissions(req, res, next).then(() => { 
     expect(UserPermission.findAll).to.have.been.calledWithMatch({ 
      where: { accountId: req.user.tenant() } 
     }); 

     expect(next).to.have.beenCalled; 
     expect(next.lastCall.args[0].output.payload.statusCode).to.equal(200); 
     expect(next.lastCall.args[0].output.payload.permission).to.include(nonAdminPermission.permission); 
     }); 
    }); 
    }) 

的错误,我得到:

Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test. 

console.log不会被调用,但我可以证实,该代码被称为与适当的参数,可以等。

回答

0

它看起来像next继续被传递为错误处理程序(第二个参数)为then方法。

我想你想:

export function listCurrentUserPermissions(req, res, next) { 
    return UserPermission.findAll({ 
    where: { accountId: req.user.tenant() } 
    }).catch((error) => { 
    console.log('-----------------------------------'); 
    console.log(error); 
    console.log.bind(console); 
    console.log('-----------------------------------'); 
    }).then((permissions) => { 
    return res.json({ userPermission: permissions.map(serializeUserPermission) }); 
    }).then(next); 
+0

不幸的是这种重构:( –

+0

哦发生同样的错误,woops不能通过'next'作为成功回调,因为它的节点式的(错误结果) (()=> {next()})' – jasonkarns

+0

虽然你通常不需要调用'next',因为这条路线是最后一行(它发送一个响应),不需要调用后续的中间件,所以我通常在promise上调用'.done'。 – jasonkarns