Bluebird Promise 在任何情况下都会得到解决

Bluebird Promise becomes resolved in any case

我一定是做错了什么,但这是我的测试用例:

const { describe, it } = require('mocha'),
  should = require('should'),
  Promise = require('bluebird') //v3.4.6
describe('Bluebird', () => {
  it('Promise is never resolved but does it get resolved?', () => {
    new Promise(() => false)
      .should.be.fulfilled() // It really shouldn't be
  })
})

这个通过了,但它不应该失败吗?

在 mocha 测试中使用 promises 时,return the promise 从测试中很重要。

在你的情况下,那将是:

it('Promise is never resolved but does it get resolved?', () => {
   return new Promise(() => false)
    .should.be.fulfilled()
})

但是,这可能仍然不是您在这里所需要的,因为在调用 should 时无法确定承诺的实现。您的实际测试可能有所不同,最重要的部分仍然是 return 承诺链。

当你这样做时,你不需要进一步断言承诺的 fulfillment/rejection,因为这是由 mocha 隐式完成的。

我个人是 chai-as-promised 的超级粉丝,它允许您使用与之前完全相同的测试,但这次,它会起作用。