我正在编写一个单元测试来测试我的postgres模式。我使用node-pg,mocha,sinon和chai。当失败时测试不会调用done()
这工作 - 测试通过没有问题:
describe('When adding a user',()=> {
it('should reject since email is used somewhere else', (done)=> {
pool.query(`INSERT INTO users(email, id, token)
VALUES($1, $2, $3)`, ['[email protected]', '12346', 'fooToken'])
.then((result)=> {
console.log('nothing in here runs, you will not see this');
done()
})
.catch((result) => {
result.constraint.should.have.string('email_already_exists');
done();
})
})
});
但是,为了确保我没有得到一个误报,我改变断言到result.constraint.should.not.have.string('email_already_exists');
故意使测试失败。
而不是测试失败,我得到Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.
。
我在做什么?
如果您正在测试基于承诺的代码,您应该考虑使用Mocha的内置[promises支持](https://mochajs.org/#working-with-promises)。更容易防止这样的问题。 – robertklep
@robertklep这个承诺如何支持在节点中测试2次读取? http://stackoverflow.com/questions/43690868/how-to-assert-stubbed-fetch-more-than-once/43806205#43806205 – dman
一个很好的例子:https://coderwall.com/p/axugwa/cleaning-这个问题中的数据库之间的摩卡测试与pg-promise –