Unit testing 玩笑模拟函数?

Unit testing 玩笑模拟函数?,unit-testing,email,jestjs,Unit Testing,Email,Jestjs,刚开始使用jest,文档中关于模拟的内容似乎不太清楚 我有一个模块,代码如下。如果我想测试发送邮件功能,但不是通过邮枪发送实际的电子邮件,我需要在这里做什么?我认为在我的测试文件中,我需要使用模拟行为,但无法确定如何具体阻止邮件发送,同时也能够检查是否遵循了正确的路径,例如无效的电子邮件地址、抛出错误等。我是否需要进一步分解此功能 const Mailgun = require('machinepack-mailgun'); const Emailaddresses = require('mac

刚开始使用jest,文档中关于模拟的内容似乎不太清楚

我有一个模块,代码如下。如果我想测试发送邮件功能,但不是通过邮枪发送实际的电子邮件,我需要在这里做什么?我认为在我的测试文件中,我需要使用模拟行为,但无法确定如何具体阻止邮件发送,同时也能够检查是否遵循了正确的路径,例如无效的电子邮件地址、抛出错误等。我是否需要进一步分解此功能

const Mailgun = require('machinepack-mailgun');
const Emailaddresses = require('machinepack-emailaddresses');
const config = require('../Config');


// Function to send an email
const sendEmail = function (toaddress, toname, sub, msg, cb) {
// Validate the email address is correct and if so send an email. Otherwise log the error and exit.
  Emailaddresses.validate({
    string: toaddress,
  }).exec({
    error(err) {
      return cb(err, null);
    },
    invalid() {
      return cb(new Error('Email address is not valid.'), null);
    },
    success() {
      Mailgun.sendPlaintextEmail({
        apiKey: config.sender.apiKey,
        domain: config.sender.domain,
        toEmail: toaddress,
        toName: toname,
        subject: sub,
        message: msg,
        fromEmail: config.sender.fromEmail,
        fromName: config.sender.fromName,
      }).exec({
        error(err) {
          return cb(err, null);
        },
        success() {
          return cb(null, 'Email Sent.');
        },
      });
    },
  });
};

module.exports.sendEmail = sendEmail;

您可以使用自己的实现模拟
Mailgun
,其中:

const Mailgun = require('machinepack-mailgun');
jest.mock('machinepack-mailgun', () = > ({
  sendPlaintextEmail: jest.fn()
}))
it('sends mail and fail', () = > {
  // no add the way `sendPlaintextEmail` should react.
  // in this case return `exec` which always run the `error` part
  Mailgun.sendPlaintextEmail.mockImplementation(() = > ({
    exec: (arg) = > {
      arg.error('someError')
    }
  }))
  const cb = jest.fn()
  sendEmail ('toaddress', 'toname', 'sub', 'msg', cb) 
  expect(Mailgun.sendPlaintextEmail).toHaveBeenCalledWith(...)
  expect(cb).toHaveBeenCalledWith(...)
})
在上面的示例中,我们模拟了mailgun模块,因此
sendplantextmail
是间谍。然后我们将模块导入到我们的测试中,这样我们就可以在每个测试中设置spy的模拟实现。在本例中,我们设置了行为,使其返回
exec
方法,然后由代码使用
error
/
success
对象调用该方法。模拟然后只调用
错误
部分。之后,您可以首先测试是否使用正确的参数调用了
Mailgun.sendplantextmail
,然后可以测试
cb
是否使用
“someError”
null
调用


在另一个测试中,您可以设置
exec
的行为,这样它将调用
success
方法。

您可以使用自己的实现模拟
Mailgun

const Mailgun = require('machinepack-mailgun');
jest.mock('machinepack-mailgun', () = > ({
  sendPlaintextEmail: jest.fn()
}))
it('sends mail and fail', () = > {
  // no add the way `sendPlaintextEmail` should react.
  // in this case return `exec` which always run the `error` part
  Mailgun.sendPlaintextEmail.mockImplementation(() = > ({
    exec: (arg) = > {
      arg.error('someError')
    }
  }))
  const cb = jest.fn()
  sendEmail ('toaddress', 'toname', 'sub', 'msg', cb) 
  expect(Mailgun.sendPlaintextEmail).toHaveBeenCalledWith(...)
  expect(cb).toHaveBeenCalledWith(...)
})
在上面的示例中,我们模拟了mailgun模块,因此
sendplantextmail
是间谍。然后我们将模块导入到我们的测试中,这样我们就可以在每个测试中设置spy的模拟实现。在本例中,我们设置了行为,使其返回
exec
方法,然后由代码使用
error
/
success
对象调用该方法。模拟然后只调用
错误
部分。之后,您可以首先测试是否使用正确的参数调用了
Mailgun.sendplantextmail
,然后可以测试
cb
是否使用
“someError”
null
调用


在另一个测试中,您可以设置
exec
的行为,这样它将调用
success
方法。

谢谢,今晚我将回顾此内容,如果没有其他问题,请标记。感谢您的帮助似乎遇到了错误错误:expect(jest.fn())。toHaveBeenCalledWith(expected)expected mock函数调用时使用:[”chris@gmail.com“,”克里斯“,”主题“,”信息“],但它没有被调用。您是否可以通过添加
console.log(Mailgun.sendplantextmail)
来检查是否调用了电子邮件验证的成功功能。它打印的是什么?而且
已经被调用了
应该是这样的:
{apiKey:config.sender.apiKey,域:config.sender.domain,toEmail:toaddress,toName:toName,sub,message:msg,fromEmail:config.sender.fromEmail,fromName:config.sender.fromName,}
而不是数组。看来我已经成功了。我希望我能告诉你原因!对我来说,在mailgun中模拟无效和错误路由的最佳方法是什么?我是否只需在模拟实现中添加其他方法?谢谢,我今晚将对此进行回顾,如果没有进一步的问题,请标记。感谢您的帮助,我似乎遇到了错误e错误:expect(jest.fn())。toHaveBeenCalledWith(expected)应使用以下函数调用模拟函数:[”chris@gmail.com“,”chris“,”subject“,”message“]但它并没有被调用。您能否通过添加
console.log(Mailgun.sendplantextmail)来检查是否调用了电子邮件验证的成功函数
。它打印的是什么?而且
已经被调用了
应该是这样的:
{apiKey:config.sender.apiKey,域:config.sender.domain,toEmail:toaddress,toName:toName,sub,message:msg,fromEmail:config.sender.fromEmail,fromName:config.sender.fromName,}
而不是数组。看来我已经成功了。我希望我能告诉你原因!对我来说,在mailgun中模拟无效和错误路由的最佳方法是什么?我只是在模拟实现中添加其他方法吗?