我正在尝试测试这种简单的表达中间件功能
function onlyInternal (req, res, next) {
if (!ReqHelpers.isInternal(req)) {
return res.status(HttpStatus.FORBIDDEN).send() <-- TRYING TO ASSERT THIS LINE
}
next()
}
这是我目前的考试
describe.only('failure', () => {
let resSpy
before(() => {
let res = {
status: () => {
return {
send: () => {}
}
}
}
resSpy = sinon.spy(res, 'status')
})
after(() => {
sinon.restore()
})
it('should call next', () => {
const result = middleware.onlyInternal(req, resSpy)
expect(resSpy.called).to.be.true
})
})
我收到此错误:TypeError: res.status is not a function
为什么res.status不起作用?在我看来,这显然是一个功能。.
答案 0 :(得分:1)
sinon.spy
返回新创建的间谍,而不是应用新间谍的res
。
因此,在您的情况下:resSpy === res.status
而不是您所期望的resSpy === res
,那没有道理。
换句话说,您仍应将原始res
传递给中间件:
const result = middleware.onlyInternal(req, res);