title: Mocking email function in nodejs
body: <p>I've got a mailer function I've built and trying to shore up the coverage. Trying to test parts of it have proven tricky, specifically this mailer.smtpTransport.sendMail </p>
  
  <pre><code>var nodemailer = require('nodemailer')
  
  var mailer = {}
  
  mailer.smtpTransport = nodemailer.createTransport('SMTP', {
      'service': 'Gmail',
      'auth': {
          'XOAuth2': {
              'user': 'test@test.com',
              'clientId': 'googleClientID',
              'clientSecret': 'superSekrit',
              'refreshToken': '1/refreshYoSelf'
          }
      }
  })
  var mailOptions = {
      from: 'Some Admin &lt;test@tester.com&gt;',
  }
  
  mailer.verify = function(email, hash) {
      var emailhtml = 'Welcome to TestCo. &lt;a href="'+hash+'"&gt;Click this '+hash+'&lt;/a&gt;'
      var emailtxt = 'Welcome to TestCo. This  is your hash: '+hash
      mailOptions.to = email
      mailOptions.subject = 'Welcome to TestCo!'
      mailOptions.html = emailhtml
      mailOptions.text = emailtxt
      mailer.smtpTransport.sendMail(mailOptions, function(error, response){
          if(error) {
              console.log(error)
  
          } else {
              console.log('Message sent: '+response.message)
          }
      })
  }
  </code></pre>
  
  <p>I'm unsure of how to go about testing, specifically ensuring that my mailer.smtpTransport.sendMail function is passing the correct parameters without actually sending the email. I'm trying to use <a href="https://github.com/whatser/mock-nodemailer/tree/master" rel="noreferrer">https://github.com/whatser/mock-nodemailer/tree/master</a>, but I'm probably doing it wrong. Should I be mocking out the method?</p>
  
  <pre><code>var _ = require('lodash')
  var should = require('should')
  var nodemailer = require('nodemailer')
  var mockMailer = require('./helpers/mock-nodemailer')
  var transport = nodemailer.createTransport('SMTP', '')
  
  var mailer = require('../../../server/lib/account/mailer')
  
  describe('Mailer', function() {
      describe('.verify()', function() {
          it('sends a verify email with a hashto an address when invoked', function(done) {
              var email ={
                  'to': 'dave@testco.com',
                  'html': 'Welcome to Testco. &lt;a href="bleh"&gt;Click this bleh&lt;/a&gt;',
                  'text': 'Welcome to Testco. This  is your hash: bleh',
                  'subject': 'Welcome to Testco!'
              }
  
              mockMailer.expectEmail(function(sentEmail) {
              return _.isEqual(email, sentEmail)
              }, done)
              mailer.verify('dave@testco.com','bleh')
              transport.sendMail(email, function() {})
      })
  })
  </code></pre>