7

使用Mochachai-as-promised,我正在尝试测试我的承诺是否被正确解决和拒绝。但是expectchai-as-promised 提供的功能并没有正确地导致测试失败。例子:

测试.js

const chai = require('chai')
chai.use(require('chai-as-promised'))
const expect = chai.expect

describe('foo', () => {
  it('resolve expected', () => {
    expect(new Promise((res,rej) => {res()})).to.be.fulfilled
  })
  it('resolve unexpected', () => {
    expect(new Promise((res,rej) => {res()})).to.be.rejected
  })
  it('reject expected', () => {
    expect(new Promise((res,rej) => {rej()})).to.be.rejected
  })
  it('reject unexpected', () => {
    expect(new Promise((res,rej) => {rej()})).to.be.fulfilled
  })
})

当我执行mocha test.js

  foo
    ✓ resolve expected
    ✓ resolve unexpected
(node:2659) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): AssertionError: expected promise to be rejected but it was fulfilled with undefined
(node:2659) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
    ✓ reject expected
    ✓ reject unexpected
(node:2659) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 4): AssertionError: expected promise to be fulfilled but it was rejected with undefined


  4 passing (10ms)

你可以看到断言错误似乎被抛出了,但是 mocha 没有发现它们。如何让 mocha 识别失败?

4

3 回答 3

10

正如我们确认的那样,为了将来参考,问题在于您没有在每个测试中返回断言。

it('reject expected', () => {
  return expect(new Promise((res,rej) => {rej()})).to.be.rejected
})
it('reject unexpected', () => {
  return expect(new Promise((res,rej) => {rej()})).to.be.fulfilled
})

这就是为什么测试通过但后来在它最终返回时打印了一个“未处理”的承诺。return关键字通知 mocha 等待异步函数解析/拒绝。

于 2017-12-18T16:46:57.367 回答
1

或者使用 chai-as-promised 库,您可以尝试使用 chai.should 并调用 .notify 以指示其“完成”。像这样:

const chai = require('chai');
chai.use(require('chai-as-promised'));
chai.should();

const expect = chai.expect;

describe('foo', () => {
    it('resolve expected', () => {
        expect(new Promise((res, rej) => { res(); })).to.be.fulfilled;
    });
    it('resolve unexpected', (done) => {
        // expect(new Promise((res, rej) => { res(); })).to.be.rejected;
        new Promise((res, rej) => { res(); }).should.be.rejected.and.notify(done);
    });
    it('reject expected', (done) => {
        // expect(new Promise((res, rej) => { rej(); })).to.be.rejected;
        new Promise((res, rej) => { rej(); }).should.be.rejected.and.notify(done);
    });
    it('reject unexpected', (done) => {
        // expect(new Promise((res, rej) => { rej(); })).to.be.fulfilled;
        new Promise((res, rej) => { rej(); }).should.be.fulfilled.and.notify(done);
    });
});

这样 mocha 将识别测试失败:

foo
    √ resolve expected
    1) resolve unexpected
    √ reject expected
    2) reject unexpected


  2 passing (37ms)   2 failing

  1) foo
       resolve unexpected:
     AssertionError: expected promise to be rejected but it was fulfilled with undefined


  2) foo
       reject unexpected:
     AssertionError: expected promise to be fulfilled but it was rejected with undefined

在此处查看 chai-as-promised 文档以获取更多参考

于 2017-12-19T06:27:02.850 回答
0
 it ('creates images', async () => {
      
      assert.equal(imageCount,1)
      const event = result.logs[0].args
      assert.equal(event.id.toNumber(),imageCount.toNumber(),'id is correct')
      assert.equal(event.hash,hash,'Hash is correct')
      assert.equal(event.description,'Image Description','description is correct')
      assert.equal(event.tipAmount,'0','tip amount is correct')
      assert.equal(event.author, author, 'author is correct')

      await decentragram.uploadImage('','Image description', { from: author }).should.be.rejected;
      await decentragram.uploadImage('Image hash','', { from:author }).should.be.rejected;
    })

在这里面临同样的问题。但同意 [luke-stoward][1]

解决方法:添加return

it ('creates images', async () => {
     return
      assert.equal(imageCount,1)
      const event = result.logs[0].args
      assert.equal(event.id.toNumber(),imageCount.toNumber(),'id is correct')
      assert.equal(event.hash,hash,'Hash is correct')
      assert.equal(event.description,'Image Description','description is correct')
      assert.equal(event.tipAmount,'0','tip amount is correct')
      assert.equal(event.author, author, 'author is correct')

      await decentragram.uploadImage('','Image description', { from: author }).should.be.rejected;
      await decentragram.uploadImage('Image hash','', { from:author }).should.be.rejected;
    })
于 2021-09-09T22:50:57.113 回答