在nodejs中模拟电子邮件功能

uxt*_*xtx 17 node.js nodemailer

我已经建立了一个邮件程序功能,并试图加强覆盖范围.试图测试它的一部分已被证明是棘手的,特别是这个mailer.smtpTransport.sendMail

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 <test@tester.com>',
}

mailer.verify = function(email, hash) {
    var emailhtml = 'Welcome to TestCo. <a href="'+hash+'">Click this '+hash+'</a>'
    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)
        }
    })
}
Run Code Online (Sandbox Code Playgroud)

我不确定如何进行测试,特别是确保我的mailer.smtpTransport.sendMail函数传递正确的参数而不实际发送电子邮件.我正在尝试使用https://github.com/whatser/mock-nodemailer/tree/master,但我可能做错了.我应该嘲笑这个方法吗?

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. <a href="bleh">Click this bleh</a>',
                '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() {})
    })
})
Run Code Online (Sandbox Code Playgroud)

riz*_*oro 15

您可以在测试中使用"存根"传输层而不是SMTP.

var stubMailer = require("nodemailer").createTransport("Stub"),
    options = {
        from: "from@email.com",
        to: "to@email.com",
        text: "My Message!"
    };

   stubMailer.sendMail(options, function(err, response){
     var message = response.message;
   })
Run Code Online (Sandbox Code Playgroud)

因此,在这种情况下,"消息"将是文本格式的电子邮件.像这样的东西:

MIME-Version: 1.0
X-Mailer: Nodemailer (0.3.43; +http://www.nodemailer.com/)
Date: Fri, 25 Feb 2014 11:11:48 GMT
Message-Id: <123412341234.e23232@Nodemailer>
From: from@email.com
To: to@email.com
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

My Message!
Run Code Online (Sandbox Code Playgroud)

有关更多示例,请查看nodemailer测试套件:https: //github.com/andris9/Nodemailer/blob/master/test/nodemailer-test.js

  • 这个测试有什么意义?像这样只是测试nodemailer库,它已经在nodemailer测试套件中进行了测试.为什么不设置虚拟SMTP服务器并实际测试预期的功能? (5认同)

Alo*_*mon 2

这个例子对我来说效果很好

======== myfile.js ========

// SOME CODE HERE

transporter.sendMail(mailOptions, (err, info) => {
  // PROCESS RESULT HERE
});
Run Code Online (Sandbox Code Playgroud)

======== myfile.spec.js(单元测试文件)========

const sinon = require('sinon');
const nodemailer = require('nodemailer');
const sandbox = sinon.sandbox.create();

describe('XXX', () => {
  afterEach(function() {
    sandbox.restore();
  });

  it('XXX', done => {
    const transport = {
      sendMail: (data, callback) => {
        const err = new Error('some error');
        callback(err, null);
      }
    };
    sandbox.stub(nodemailer, 'createTransport').returns(transport);

    // CALL FUNCTION TO TEST

    // EXPECT RESULT
  });
});
Run Code Online (Sandbox Code Playgroud)