javascript - Firebase 功能 auth user onCreate uni 测试
问题描述
您将如何实现对以下调用的存根?
我已经设法存根用户对象
admin.firestore().collection('roles').where('role', '==', 'unassigned');
功能
exports.userRegistration = functions.auth.user()
.onCreate((user, context) => {
let unassignedRole;
let roleRef = admin.firestore().collection('roles').where('role', '==', 'unassigned');
await roleRef.get()
.then(snapshot => {
snapshot.forEach(doc => {
unassignedRole = doc.ref;
});
})
.catch(err => {
console.log('Error getting documents', err);
});
await admin.firestore().collection('users').add({
firstName: user.firstName || "",
lastName: user.lastName || "",
fullName: user.displayName || "",
profilePic: user.photoURL || "",
email: user.email,
dateRegistered: admin.firestore.FieldValue.serverTimestamp(),
role: unassignedRole
});
});
单元测试
describe('Cloud Functions', () => {
let myFunctions, adminInitStub;
before(() => {
adminInitStub = sinon.stub(admin, 'initializeApp');
myFunctions = require('../index');
});
after(() => {
adminInitStub.restore();
test.cleanup();
});
describe('userRegistration', () => {
it('should write the newly registered user to /users', () => {
const mockUser = test.auth.makeUserRecord({
email: "test@email.com"
});
const wrapped = test.wrap(myFunctions.userRegistration);
})
});
});
解决方案
推荐阅读
- python - 等效于 Python 中的“qt”R 函数
- azure-devops - 如何在不部署的情况下调试和开发 Azure DevOps 扩展?
- node.js - 热键正确杀死节点进程并释放使用的端口
- vim - 设置由 ghcide 生成的多行错误消息的错误格式
- php - 在 CakePHP 3 中包含连接后加入子查询
- python - Python合并两个csv文件python
- python - 如何避免使用 python Wand 为 dds 创建 mipmap?
- azure - 如何使用 powershell 获取 Azure 虚拟机规模集实例的列表
- enterprise-architect - 操作方法/您可以在 Enterprise Architect 15 中从导入的数据库生成序列图或数据流图吗?
- python - pylint 引发错误,但代码对我有用