Skip to content

Instantly share code, notes, and snippets.

@aroman
Created May 17, 2014 15:15
Show Gist options
  • Save aroman/f08b65d7c386513f64ee to your computer and use it in GitHub Desktop.
Save aroman/f08b65d7c386513f64ee to your computer and use it in GitHub Desktop.
mocha + async test isolation
module.exports.send = function (template, model, recipient, subject, callback) {
async.waterfall([
function readTemplate (wf_callback) {
fs.readFile(path.join(__dirname, TEMPLATES_DIR, template), wf_callback);
},
function renderTemplate (data, wf_callback) {
wf_callback(null, data.toString());
},
function sendEmail (html, wf_callback) {
var params = {
from: 'noreply@example.com',
fromname: 'example.com',
to: recipient,
subject: subject,
html: html
};
sendgrid.send(params, wf_callback);
}
], function waterfallDone (err, json) {
if (err) return callback(err);
callback(null, true);
});
};
describe('email', function () {
var sandbox;
beforeEach(function () {
sandbox = sinon.sandbox.create();
});
afterEach(function () {
sandbox.restore();
});
it('should read the given template', function (done) {
var user = new User(fx.User.valid);
var template = 'some_template.html';
var stub = sandbox.stub(fs, 'readFile');
stub.callsArgWith(1, null, new Buffer(''));
email.send(template, user, user.email, 'Subject', function () {
stub.calledOnce.should.be.ok;
stub.firstCall.args[0].should.endWith(template);
stub.firstCall.args[1].should.be.a.Function;
done();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment