【发布时间】:2020-04-13 06:39:13
【问题描述】:
我正在使用 sinon.js 来测试我的 API。 我想测试正在调用的辅助函数的顺序。
controller.js
exports.controllerFunction = async (req, res) => {
const function1Results = await function1(paramm);
const function2Results = await function2(param, function1Results);
return res.send(function2Results);
};
helpers.js
exports.function1 = function(param) {
return param;
}
exports.function2 = function(param, func) {
return param;
}
unitTest.js
const controller = require('./controller.js')
const helpers = require('./helpers.js')
describe('Unit test cycle', () => {
beforeEach(() => {
// Spies
sinon.spy(controller, 'controllerFunction');
sinon.spy(helpers, 'function1');
sinon.spy(helpers, 'function2');
// Function calls
controller.controllerFunction(this.req, this.res)
})
afterEach(() => {
sinon.restore();
})
this.req = {}
this.res = {}
it('should call getAvailability', (done) => {
expect(controller.controllerFunction.calledOnce).to.be.true
expect(helpers.function1.calledOnce).to.be.true
expect(helpers.function2.calledOnce).to.be.true
});
})
expect(controller.controllerFunction.calledOnce).to.be.true
返回为 true
expect(helpers.function1.calledOnce).to.be.true
expect(helpers.function2.calledOnce).to.be.true
并且以 false 的形式出现。
因为控制器中使用了我的辅助函数,所以它们也应该被调用,但它们不是。
那么我如何测试我的辅助函数在测试控制器时是否也被调用了?
【问题讨论】:
-
您是否尝试在这些功能上设置间谍?
-
是的,我会编辑问题。谢谢!
标签: javascript node.js mocha.js sinon sinon-chai