如何测试异步可观察的订阅下一个功能

时间:2020-06-02 11:26:00

标签: node.js unit-testing rxjs sinon-chai rxjs-observables

这是我第一次尝试使用rxjsObservable。这是我的代码段:

const onError = error => console.log(error)

const onComplete = () => console.log('Completed')

const debugLog = rawMessage => console.debug('Raw message', rawMessage)

const ackMessage = rawMessage => console.info('Message acked', rawMessage)

const processEvents = async (messages) => {
  if (!messages || !messages.length) {
    return console.log('No messages to process')
  }

  try {
    await myAsyncFunc()
    console.info('Messages processed') // Unit tests, this never gets called
  } catch (err) {
    console.error(err)
  }
}

const handler = (emitter) => {
  Observable.create((observer) => {
    emitter.on('data', (data) => observer.next(data))
    emitter.on('error', (err) => observer.error(err))
    emitter.on('complete', () => observer.complete())
  }).pipe(
    tap(debugLog),
    tap(ackMessage),
    bufferTime(process.env.BATCH_TIMEOUT_MS, null, process.env.BATCH_SIZE)
  ).subscribe({
    next: processEvents,
    error: onError,
    complete: onComplete
  })
}

我正在尝试使用sinonchai编写单元测试。当我尝试测试myAsyncFunc被解决或拒绝时,console.info('Messages processed')中的console.error(err)processEvents都不会被触发。 emitter正在从EventEmitter模块扩展标准Node.js events

我的单元测试片段如下:

context('when myAsyncFunc fails', () => {
  const err = new Error('Error')
  beforeEach(() => {
    myAsyncFunc.rejects(err)
    mySubscription.handler(emitter)
    emitter.emit('data', myMessage)
    clock.tick(5000)
  })

  it('should log an error', () => {
    expect(console.error).to.have.been.calledWith(
      sinon.match.instanceOf(Error).and(sinon.match.has('message', 'Error')) // test fails
    )
  })
})

context('when myAsyncFunc succeeds', () => {
  beforeEach(() => {
    myAsyncFunc.resolves()
    mySubscription.handler(emitter)
    emitter.emit('data', myMessage)
    clock.tick(5000)
  })

  it('should log an info', () => {
    expect(console.info).to.have.been.calledWith('Messages processed') // test fails
  })
})

debugLogackMessage中的所有内容都将被执行。 process.env.BATCH_TIMEOUT_MS设置为小于5000,正在测试中使用-clock.tick(5000)

processEvents函数是否为async是否存在问题,我应该对上述processEvents函数的实现采取不同的方法吗?

0 个答案:

没有答案