我想对以下简化模块进行单元测试:
const Logger = require('logplease');
const logger = Logger.create('utils');
const tester = {
one: () => {
logger.log('called real one()');
tester.two();
},
two: () => {
logger.log('called real two()');
},
};
module.exports = {
one: tester.one,
two: tester.two
};
我正在logplease
使用Proxyquire替换外部依赖项,效果很好。但是我需要存根two()
,因为我想进行单元测试,同时消除在实际代码中运行时产生one()
的副作用。two()
it.only('stubbing functions on the "proxyquired" object under test', function(done) {
const loggerStub = {
create: () => {
return { log: (msg) => { console.log('fake logger: ', msg); } };
}
};
let tester = proxyquire('../tester', { 'logplease': loggerStub });
let stub2 = sinon.stub(
tester,
'two',
() => {
console.log('called fake stub of two()');
}
);
tester.one();
console.log('call count 2: ', stub2.callCount);
done();
});
我得到的输出:
fake logger: called real one()
fake logger: called real two()
call count 2: 0
我期望的输出:
fake logger: called real one()
called fake stub of two()
call count 2: 1
为什么我的存根函数不运行?