gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:13:44 25 4
gpt4 key购买 nike

下面我对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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com