首页 > 解决方案 > 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);
        })
    });
});

标签: javascriptgoogle-cloud-firestorefirebase-authenticationgoogle-cloud-functionssinon

解决方案


推荐阅读