【问题标题】:Readable stream `_read` not called in test测试中未调用可读流`_read`
【发布时间】:2018-03-30 11:50:01
【问题描述】:

我有以下简化的 ava 测试用例。当我通过 ava _read() 运行它时,永远不会被调用(ONDATA 可以)。另一方面,当我将此测试主体(不带断言)作为节点脚本运行时,我总是会按预期调用_read()。 可能我错过了一些关键功能,请指教。

test('...', async t => {
  class R extends stream.Readable {
    _read() { console.log('READ'); }
  }
  const rs = new R();

  rs.on('data', data => {
    console.log('ONDATA ', data.toString());
  });
  rs.push(Buffer.from('data'));
  // t.is(...)
})

【问题讨论】:

    标签: ava node-streams


    【解决方案1】:

    我不记得在什么情况下应该调用_read(),但很可能您的测试在此之前就结束了。你有一个async 测试,但你似乎没有await 任何东西。尝试返回一个明确的承诺或以其他方式使用test.cb(),以便您可以使用t.end() 结束测试。

    【讨论】:

      【解决方案2】:

      谢谢!基本上我搞砸了可读的流回调和async test。所需的测试看起来像

      test.cb('...', t => {
        class R extends stream.Readable {
          _read() {
            console.log('READ');
            // t.pass() or t.end() HERE
          }
        }
        const rs = new R();
      
        rs.on('data', data => {
          // t.pass() or t.end() HERE
          console.log('ONDATA ', data.toString());
        });
        rs.push(Buffer.from('data'));
      })
      

      【讨论】:

        猜你喜欢
        • 2018-08-25
        • 2016-11-07
        • 2014-10-16
        • 1970-01-01
        • 1970-01-01
        • 2014-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多