使用或不使用通知方法调用 chai-as-promised 有什么区别?

What is the difference between calling chai-as-promised with or without a notify method?

我正在使用 chaichai-as-promised 来测试一些异步 JS 代码。

我只是想检查一个 return 承诺的函数最终会 return 一个数组并编写了以下 2 个测试:

A:

it('should return an array', () => {
    foo.bar().should.eventually.to.be.a('array')
})

B:

it('should return an array', (done) => {
    foo.bar().should.eventually.to.be.a('array').notify(done)
})

两者都通过了 OK,但只有 B 选项实际运行了我的 bar() 函数中包含的完整代码(即显示来自代码的 console.log() 消息以下)。难道我做错了什么?为什么会这样?

bar() {
    return myPromise()
    .then((result) => {
      console.log('Doing stuff')
      return result.body.Data
    })
    .catch((e) => {
      console.err(e)
    })
  }

测试承诺意味着您正在测试异步代码。 Notify 和 done 回调设置一个计时器并等待 promise 链完成执行。

第二种方法是正确的,因为您可能需要测试链式承诺。

看看这个 tutorial 让我进入异步单元测试。

你用什么测试库? Mocha,实习生还是其他? 对于 Mocha 和 Intern,您必须 return 测试方法中的承诺:

it('should return an array', () => {
    return foo.bar().should.eventually.to.be.a('array');
})