【发布时间】:2019-03-29 12:59:42
【问题描述】:
我有一个像这样的中间件类
// code.js
function validation(req, res, next) {
if (validationLogic(req)) {
res.send(400);
return next(false);
}
return next();
}
// code.test.js
describe('validation', () => {
describe('when req is valid', () => {
//setting up req, res, next stub
//some other test
//HERE IS MY QUESTION, how do I text that validation returns next(), and not next(false)
it('return next(), and next() is called exactly once', () => {
const spy = sinon.spy();
nextStub = spy;
const result = validation(reqStub, resStub, nextStub);
assert(spy.calledOnceWithExactly());
assert(result === nextStub()); // both of this
assert(result === nextStub(false)); // and this line passed
});
});
});
我试图测试我的validation 函数是否返回next() 而不是next(false)。但是在测试中,貌似只有assert(spy.calledOnceWithExactly())可以测试next中的参数。但是assert(result === nextStub()) 后面的行不能测试任何东西,除了结果实际上来自函数next()
assert(spy.calledOnceWithExactly()) 是否足够,或者有其他方法可以测试它吗?
【问题讨论】:
标签: javascript unit-testing middleware sinon restify