javascript - 如何使用 sinon 重新创建它?

标签 javascript node.js express testing sinon

下面我对express中间件进行了测试。我希望使用 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)
  })

最佳答案

  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')
  })

关于javascript - 如何使用 sinon 重新创建它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49366576/

相关文章:

node.js - Express js中的AWS s3文件上传: whats the difference between upload vs multipart upload

angularjs - 在html5模式下使用ng-route重新加载 Angular 页面不会返回index.html

node.js - 对多个 Node 请求使用 promise

javascript - 如何在 Google Chrome DevTools 中的 lambda 调用处设置断点?

node.js - 我该如何解决 npm 错误! js错误:0909006C:PEM in Node.?

javascript - 如何使用 Node.js 解析包含 JSON 对象数组的文件?

javascript - 当对象是函数时,为什么 module.export 返回未定义?

javascript - Express.js/React.js 页面刷新状态持久化

javascript - JQuery - 用 html 转义替换引号

javascript - ng-repeat 中函数的性能