【问题标题】:Chai as promised not waiting for promise to be fulfilled柴如许不等诺言兑现
【发布时间】:2016-05-29 20:06:31
【问题描述】:

我的控制台的输出。请注意,控制台日志是乱序的(1,3,4,2 而不是 1,2,3,4)

代码在这里

  it('can store file', () => {
    console.log('1) file storage start')
    return filestore.store.q(file).then(() => {
      console.log('2) file storage done')
    }).should.eventually.be.fullfilled
  })

  describe('block number', () => {

    beforeEach(() => {
      console.log('3) check blockNumber')
      return web3.Q.all([
        web3.eth.getBlockNumber.q().then((_blockNumber) => {
          blockNumber = _blockNumber
        }),
        web3.eth.getMining.q().then((isMining) => {
        })
      ])
    })

    it('can retreive files block number', () => {
      console.log('4) retreive')
      return filestore.getBlockNumber.q(fileHash).should.eventually.bignumber.equal(blockNumber)
    })

  })

【问题讨论】:

  • filestore.store.q的代码是什么?我已经广泛使用 Chai-as-Promised。从来没有遇到过问题。
  • 以一种有意义的方式在此处添加该代码需要大量工作(它是一个非常复杂的功能)。然而,它明确地返回了一个适当的承诺,这可以通过 1)then 函数触发和 2)chai-as-promised 接受它作为已实现
  • 什么是.bignumber?
  • 它来自 chai-bignumber

标签: javascript promise mocha.js chai chai-as-promised


【解决方案1】:

结果证明这是一个愚蠢的错字。我输入了fullfilled 而不是fulfilled

【讨论】:

    【解决方案2】:

    我怀疑你从 Chai 那里得到了副作用。首先尝试不使用它进行测试,例如:

    const assert = require('assert');
    
    it('can store file', () => {
        console.log('1) file storage start')
        return filestore.store.q(file).then(() => {
          // Promise should have fulfilled. Nothing more to do.
          // Using should and chai after this is probably causing the problem.
          // But you should really add some sort of assertion here to
          // be able to detect regressions.
          console.log('2) file storage done')
        });
      });
    
      describe('block number', () => {
        let blockNumber;
    
        beforeEach(() => {
          console.log('3) check blockNumber')
          return web3.Q.all([
            web3.eth.getBlockNumber.q().then((_blockNumber) => {
              blockNumber = _blockNumber
            }),
            web3.eth.getMining.q().then((isMining) => {
            })
          ])
        })
    
        it('can retreive files block number', () => {
          console.log('4) retreive')
          return filestore.getBlockNumber.q(fileHash)
            .then((result) => {
              // I'm not sure if assert.equal will work with big numbers.
              // You might need a different comparator here.
              assert.equal(result, blockNumber, 'should equal the blocknumber));
            });
        });
      });
    

    Mocha 知道如何处理返回的 Promise,因此真的不需要 Chai。这是不必要的糖。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多