我使用mocha
来针对外部Web服务运行一些集成测试。我使用superagent-promise
作为请求/响应处理,并且我使用expect
作为我的断言库。使用superagent + promises的摩卡测试超时而不是以'expect'失败
对于其中一些测试,我需要将大量请求链接在一起,所以承诺非常有帮助。但是我注意到我的测试现在失败了,并且发生了超时错误(而没有错误信息),而不是错误信息本身。举个简单的例子:
it('[MESSAGES-1] cannot be posted without an auth token', function(done) {
agent.post(config.webRoot + '/rooms/ABC/messages').send({
content: 'This is a test!'
}).end().then(function(res) {
// Not expected
}, function(err) {
expect(err.status).toBe(401)
done()
})
})
按预期工作,并传递:
Messages
✓ [MESSAGES-1] cannot be posted without an auth token
然而,如果我改变我的说法,期待一个不同的状态代码:
expect(err.status).toBe(200) // This should fail
然后测试超时失败!
1) Messages [MESSAGES-1] cannot be posted without an auth token:
Error: timeout of 1000ms exceeded. Ensure the done() callback is being called in this test.
这是常见问题吗?是否有解决办法或调整我可以做?我不想失去使用承诺的能力。
就是这样!我是JavaScript的新承诺 - 我没有意识到catch(),但它完美地工作。 –