For the following snippet of nodejs
code, how would I stub the send
method using proxyquire
and sinon
, given that this belongs to file index.js
?
I have tried many ways but constantly get errors.
var emailjs = require("emailjs");
emailjs.server.connect({
user: obj.user,
password: obj.password,
host: obj.host,
port: obj.port,
tls: obj.tls,
ssl: obj.ssl
})
.send(mailOptions, function(error, message){
if (error) {
console.log("ERROR");
context.done(new Error("There was an error sending the email: %s", error));
return;
} else {
console.log("SENT");
context.done();
return;
}
});
So far in my tests I have the following set-up, but get Uncaught TypeError: Property 'connect' of object #<Object> is not a function
.
readFileStub = sinon.stub();
sendStub = sinon.stub();
connectStub = sinon.stub().returns(sendStub);
testedModule = proxyquire('../index', {
'fs': {readFile: readFileStub},
'emailjs': {
'server': {
'connect': {
'send': sendStub
}
}
}
});