-
-
Save tridungle/738915b9415e577e6ee2961aa2072aab to your computer and use it in GitHub Desktop.
Stubbing email sending in tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is the simplest possible approach - use a custom module to wrap a single, pre-configured | |
// mail transport instance and then use sinon.js to stub out that modules sendmail() | |
// This approach has its limitations, but it is still better than having 'if(NODE_ENV === 'test')' | |
// all over the code base. | |
// -- app/sendmail.js -- | |
var nodemailer = require('nodemailer'); | |
var config = require('config').email; | |
var transport = nodemailer.createTransport(config.transport, config.transportOptions); | |
module.exports.sendmail = function(message) { | |
return transport.sendMail(message); | |
} | |
// -- test/common.js -- | |
// modules are loaded only once and then cached, so we get the same instance every time | |
var sendmail = require('../app/sendmail'); | |
sinon.stub(sendmail, 'sendmail').yields(null, { | |
message: '250 2.0.0 OK 1403606574 og3sm31277838pbc.48 - gsmtp', | |
messageId: 'adefbd50fb8c11e3a3ac0800200c9a66@localhost' | |
}); | |
// -- in all test files -- | |
require('../../common'); // include the common setup for every test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment