繁体   English   中英

柴如诺,不等诺言兑现

[英]Chai as promised not waiting for promise to be fulfilled

我的控制台的输出。 请注意,控制台日志是乱序的(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)
    })

  })

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

我怀疑你从柴那里得到了副作用。 首先尝试在没有它的情况下进行测试,例如:

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。 这是不必要的糖。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM