我已经建立了一个邮件功能,并试图加强覆盖范围。尝试测试它的某些部分已被证明很棘手,特别是这个 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() {})
})
})