nodemailer不发送内部摩卡测试

时间:2019-12-01 21:26:04

标签: node.js mocha chai nodemailer

我在路线上宣布了这一点:

router.get('/test',userController.test);

userController中的函数是这样声明的:

exports.test = function (req, res) {
    var transport = nodemailer.createTransport({
        service: 'gmail',
        auth: {
            user: myuser,
            pass: mypass
        }
    });

    const message = {
        from: 'me@gmail.com',
        to: 'other@gmail.com',
        subject: 'Subject here',
        text: 'Text here',
        html: '<html><body><div>html here</div></body></html>'
    };
    console.log("message:",message);
    transport.sendMail(message, function(err, info) {
        if (err) {
            console.log(err)
        } else {
            console.log(info);
        }
    });

    res.status(200).json({
        test: 'test'
    });
};

现在,当我使用

启动应用程序时
node app.js

然后我会在收件箱中收到邮件。
此外,还会在控制台中打印“信息”:

message: {
from: 'me@gmail.com',
to: 'other@gmail.com',
subject: 'Subject',
text: 'text here',
html: '<html><body><div>hmtl here</div></body></html>'
}   
{
  accepted: [ 'other@gmail.com' ],
  rejected: [],
  envelopeTime: 174,
  messageTime: 374,
  messageSize: 675,
  response: '250 2.0.0 OK  1575235049 y6sm13785566ljm.95 - gsmtp',
  envelope: {
    from: 'me@gmail.com',
    to: [ 'other@gmail.com' ]
  },
  messageId: '<somehash@gmail.com>'

所以看起来不错。 但是,当我通过mocha运行此程序时,收件箱中没有邮件到达。控制台中没有打印任何信息。 如您所见,如果有错误应该打印出来。但这也不打印。 但是运行测试时,我在控制台中看到了

的结果
console.log("message:",message);

但是仍然没有邮件发送。看来sendMail函数从未实现/执行。

可能是什么问题?
测试模块只是调用测试端点(这里调用了其他端点,并且所有端点都已执行):

//This test is green
it("Succedes when test", done => {
chai
  .request(app)
  .get("/users/test")
  .end((err, res) => {
    expect(res).to.have.status(200);
    done();
  });
});

1 个答案:

答案 0 :(得分:1)

通常,此类问题是由于未捕获的异常或未捕获的Promise拒绝导致的。因此,根本不会显示错误。

就您而言,您似乎没有检查 c = Connection('sid', config=config) c.put("local", "/etc/nginx/sites-available/addr", use_sudo=True) 回调~/.virtualenvs/fabric/lib/python3.7/site-packages/fabric/connection.py in put(self, *args, **kwargs) 784 .. versionadded:: 2.0 785 """ --> 786 return Transfer(self).put(*args, **kwargs) 787 788 # TODO: yield the socket for advanced users? Other advanced use cases TypeError: put() got an unexpected keyword argument 'use_sudo' 参数中是否有错误。 尝试先检查.end是否不存在。

err