问题描述
我已经构建了一个邮件程序功能并试图支持覆盖范围.尝试测试它的一部分已经证明很棘手,特别是这个 mailer.smtpTransport.sendMail
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
var nodemailer = require('nodemailer')
var mailer = {}
mailer.smtpTransport = nodemailer.createTransport('SMTP', {
'service': 'Gmail',
'auth': {
'XOAuth2': {
'user': '[email protected]',
'clientId': 'googleClientID',
'clientSecret': 'superSekrit',
'refreshToken': '1/refreshYoSelf'
}
}
})
var mailOptions = {
from: 'Some Admin <[email protected]>',
}
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,但我可能做错了.我应该嘲笑这个方法吗?
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 https://github.com/whatser/mock-nodemailer/tree/master, but I'm probably doing it wrong. Should I be mocking out the method?
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': '[email protected]',
'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('[email protected]','bleh')
transport.sendMail(email, function() {})
})
})
推荐答案
您可以在测试中使用存根"传输层而不是 SMTP.
You can use a 'Stub' transport layer on your test instead of SMTP.
var stubMailer = require("nodemailer").createTransport("Stub"),
options = {
from: "[email protected]",
to: "[email protected]",
text: "My Message!"
};
stubMailer.sendMail(options, function(err, response){
var message = response.message;
})
因此,在这种情况下,消息"将是文本格式的电子邮件.像这样:
So, in that case, 'message' will be the email in text format. Something like this:
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: [email protected]
To: [email protected]
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
For more examples, take a look at nodemailer test suite:https://github.com/andris9/Nodemailer/blob/master/test/nodemailer-test.js
这篇关于nodejs中的模拟电子邮件功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!