node.js - googleApi 的单元测试
问题描述
我有以下代码,我正在尝试创建单元测试以确保 sendToGoogle 函数正常工作,所以我存根 gmail.users.messages 的 send 方法并期望该方法由 sendToGoogle 方法调用,但我收到了这个错误
AssertionError:预计发送至少被调用一次,但从未被调用
//电子邮件.js
const helper = require('./helper')
/**
* sendEmail - sends an email to a given address
*
* @param {String} to - The address of the recipient.
* @param {String} from - The address of the sender.
* @param {String} subject - subject of the email.
* @param {String} bodyText - text of the email.
**/
function sendEmail(to, from, subject, bodyText) {
const oAuthClient = helper.getAuth(process.env.CLIENT_ID, process.env.PRIVATE_KEY, from);
const emailLines = helper.createEmail(to, from, subject, bodyText);
helper.sendToGoogle(oAuthClient, from, emailLines);
}
module.exports.sendEmail = sendEmail;
这是 helper.js,其中包含我的代码
//helper.js
const {google} = require('googleapis');
const base64 = require('base-64');
const gmail = google.gmail("v1");
/**
* createEmail - createEmail an email message
*
* @param {String} to - The address of the recipient.
* @param {String} from - The address of the sender.
* @param {String} subject - subject of the email.
* @param {String} bodyText - text of the email.
* @return {String} - Message to send.
**/
const createEmail = function (to, from, subject, bodyText) {
const emailLines = ["Content-Type: text/plain; charset=\"UTF-8\"\n",
"MIME-Version: 1.0\n",
"Content-Transfer-Encoding: 7bit\n",
"to: ", to, "\n",
"from: ", from, "\n",
"subject: ", subject, "\n\n",
bodyText
].join('')
const messageBase64 = base64.encode(emailLines.trim()).replace(/\+/g, '-').replace(/\//g, '_');
return messageBase64
}
/**
*
*
* @param {String} clientKey
* @param {String} privateKey
* @param {String} from
* @returns
*/
const getAuth = function (clientKey, privateKey, from) {
return new google.auth.JWT(
clientKey,
null,
privateKey,
['https://www.googleapis.com/auth/gmail.send'],
from
);
}
/**
* @param {String} oAuthClient
* @param {String} from
* @param {String} message
*/
const sendToGoogle = function (oAuthClient, from, message) {
console.log("sendtogoogle calllled")
gmail.users.messages.send({
auth: oAuthClient,
userId: from,
resource: {
raw: message
}
}, function (err, resp) {
if (!err) {
return resp
}
console.error(err)
});
}
module.exports.createEmail = createEmail
module.exports.getAuth = getAuth
module.exports.sendToGoogle = sendToGoogle
这是单元测试文件
//email.spec.js
it ("send to gmail",function(){
const gmail = google.gmail("v1");
const SendStub = sinon.stub(gmail.users.messages, 'send').returns('test')
const result = helper.sendToGoogle("oAuthClient", from,"message")
SendStub.should.have.been.called
})
感谢您的帮助。
解决方案
Sinon 文档没有更新,我也浪费了太多时间,找到了适合我的解决方案。
sandbox.stub(gmail.users.messages, 'send').callsFake(() => {
return 'test';
});
推荐阅读
- ios - 动画约束时无法同时满足约束
- docker - 无法在所有投影运行的情况下启动 dockerized 事件存储
- java - 返回 Intent 的按钮
- node.js - 从超级账本作曲家 v0.16 升级到 v0.20.2
- php - 如何在 PHP 的 POST 请求中获取 JSON
- python - TypeError:无法处理此数据类型,试图从 numpy 获取二值化图像
- java - Spring - 如何运行一系列线程并等待它们完成后再完成?
- css - 如何通过 NPM 分发 CSS
- javascript - 将数组转换为 JS 对象
- android - 让 Instant Preview 与 Unity 和 Android 一起使用