如何使用sinon重新创建这个?

时间:2018-03-19 15:25:45

标签: javascript node.js express testing sinon

下面我对快速中间件进行了测试。我希望使用sinon来模拟这些回调,而不是为变量保持全局状态。

  it('should return html', async () => {
    const val = []
    const req = {}
    const res = {send: (recieved) => {val.push(recieved)}}
    const next = (e) => {throw e}
    expect(val).to.be.length(0)
    await expressMiddleware(alpha)(req, res, next)
    const expectation = 'response sent'
    expect(val).to.be.length(1)
    expect(val[0]).to.be(expectation)
  })

1 个答案:

答案 0 :(得分:0)

  it('should return html', async () => {
    const res = {
      send: sinon.spy()
    }
    const next = sinon.spy()
    await expressMiddleware(alpha)({}, res, next)
    expect(res.send.called).to.be.length(true)
    expect(res.send.args[0][0]).to.be('response sent')
  })