【发布时间】:2018-12-26 17:16:21
【问题描述】:
我正在尝试为这个简单的中间件函数编写一个独立的测试
function onlyInternal (req, res, next) {
if (!ReqHelpers.isInternal(req)) {
return res.status(HttpStatus.FORBIDDEN).send()
}
next()
}
// Expose the middleware functions
module.exports = {
onlyInternal
}
这不起作用
describe('success', () => {
let req = {
get: () => {return 'x-ciitizen-token'}
}
let res = {
status: () => {
return {
send: () => {}
}
}
}
function next() {}
let spy
before(() => {
spy = sinon.spy(next)
})
after(() => {
sinon.restore()
})
it('should call next', () => {
const result = middleware.onlyInternal(req, res, next)
expect(spy.called).to.be.true <-- SPY.CALLED IS ALWAYS FALSE EVEN IF I LOG IN THE NEXT FUNCTION SO I KNOW IT'S GETTING CALLED
})
})
但确实如此..
describe('success', () => {
let req = {
get: () => {return 'x-ciitizen-token'}
}
let res = {
status: () => {
return {
send: () => {}
}
}
}
let next = {
next: () => {}
}
let spy
before(() => {
spy = sinon.spy(next, 'next')
})
after(() => {
sinon.restore()
})
it('should call next', () => {
const result = middleware.onlyInternal(req, res, next.next)
expect(spy.called).to.be.true
})
})
为什么不能只监视函数工作?
【问题讨论】:
标签: javascript unit-testing mocha.js sinon chai