20

私はメーラー機能を構築し、カバレッジを強化しようとしています。その一部をテストしようとすると、特にこの 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)
        }
    })
}

具体的には、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() {})
    })
})
4

4 に答える 4

19

テストでは、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;
   })

したがって、その場合、「メッセージ」はテキスト形式の電子メールになります。このようなもの:

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!

その他の例については、nodemailer テスト スイートをご覧ください: https://github.com/andris9/Nodemailer/blob/master/test/nodemailer-test.js

于 2014-02-25T14:12:00.790 に答える