If create an file with the following contents
const validateEmail = email => {
sendEmail(email);
};
const sendEmail = email => {
return true;
};
module.exports = {
validateEmail,
sendEmail,
};
And a test that tries to stub out the second function...
it('Should call sendEmail if a valid email is passed', () => {
let sendEmailSpy = sinon.stub(checkEmail, 'sendEmail');
checkEmail.validateEmail('[email protected]');
assert.isTrue(sendEmailSpy.called);
});
It still calls the sendEmail
function and the test fails
However, if I write the module.exports
like this:
module.exports = {
validateEmail(email) {
this.sendEmail(email);
},
sendEmail(email) {
return true;
},
};
It stubs it correctly...Why?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…