简体   繁体   English

如何将单元测试中的上下文参数发送到在函数模拟器上运行的 firebase 函数

[英]How to send context parameter in unit test to a firebase functions running on the functions emulator

I manage to start the firebase emulators and load a cloud function.我设法启动了 firebase 模拟器并加载了一个云功能。 Now I want to write a test.现在我想写一个测试。

PROBLEM I use chai-http to call the function in the emulator but I do not succeed in sending a context to the function.问题我使用chai-http在模拟器中调用函数,但我没有成功地向函数发送context

when calling the function using chai-http , I see the following warning and error :使用chai-http调用函数时,我看到以下警告和错误:

{"severity":"WARNING","message":"Request body has extra fields: context"} {"severity":"ERROR","message":"Invalid request, unable to process."} {"severity":"WARNING","message":"Request body has extra fields: context"} {"severity":"ERROR","message":"Invalid request, unable to process."}

Here is the test code snippet :这是测试代码片段:

    it("function_call", async() => {
        await new Promise((resolve, reject) => {
            chai.request(url)
                .post("")
                .send({
                    data: {
                        id: FILE_ID
                    },
                    context: {
                        auth: {
                            uid: USER_ID,
                            token: USER_TOKEN
                        }
                    }
                })
                .end(function(err, res) {
                    console.info(JSON.stringify(res));
                    const payload = JSON.parse(res.text);
                    chai.expect(payload.error).not.null;
                    resolve();
                });

        });
        // expect some data from firestore emulator to be deleted
        const afterCAll = await firestore.collection(`users/${USER_ID}/files/${FILE_ID}`).get();
        chai.expect(afterCAll.empty).is.true;
    });

And here is the function code :这是功能代码:

export const doSomething = async(data, context) => {

    console.log("context=" + JSON.stringify(context));
    console.log("context.auth=" + JSON.stringify(context.auth));

}


export const CLOUD_FUNCTION = functions
    .runWith(runtimeOpts)
    .region("REGION")
    .https
    .onCall(doSomething);

And to execute the test, i run :为了执行测试,我运行:

firebase emulators:exec --project dev-export 'npm test --prefix functions --verbose --debug'

Leaving out the context in the parameter :省略参数中的上下文:

            chai.request(url)
                .post("")
                .send({
                    data: {
                        id: FILE_ID
                    }
                })

and the function call to the emulator works just fine并且对模拟器的函数调用工作正常

Looking for similar cases with the error you provided, I have a found a question that tries to get user credentials from context, and I noticed the functions they were using were not asynchronous, you might want to check context , and authData .寻找与您提供的错误类似的情况,我发现了一个试图从上下文中获取用户凭据的问题,我注意到他们使用的函数不是异步的,您可能想要检查contextauthData You can try:你可以试试:

exports.doSomething = functions.https.onCall((data, context) => {
    const result = context;
    const result= context.auth;
    console.log("context=" + JSON.stringify(result));
    console.log("context.auth=" + JSON.stringify(resultAuth));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何设置用于单元测试的测试套件 Firebase 与模拟器连接的功能,特别是 Firestore 模拟器? - How to setup a test suit for unit testing Firebase Functions that connect with emulators, specifically the Firestore emulator? 如何对 onCall firebase 云功能进行单元测试? - How to unit test onCall firebase cloud functions? 如何将列表作为参数发送到Flutter中的Firebase函数? - How to send a list as a parameter to Firebase Functions in Flutter? firebase 函数模拟器:context.auth 未定义 - firebase functions emulator: context.auth is undefined 如何使用 Firebase 模拟器从单元测试中调用 Firebase 函数? - How to call a firebase functions from a unit test using Firebase Emulators? 如何使用 Firebase 模拟器通过 Firestore 触发器测试 Java 云功能? - How to use Firebase emulator to test Java cloud functions with Firestore trigger? Firebase 模拟器不促进/运行新功能 - Firebase emulator not facilitating/running new functions 有没有办法向正在运行的 firebase 仿真器添加/修改功能? - Is there a way to add/modify functions to a running firebase emulator? 运行 Firebase 函数测试时“路径”中的类型错误 - TypeError in "path" when running Firebase Functions Test 为什么我的 Firebase Functions 模拟器没有运行我在 Typescript 中所做的更改? - Why is my Firebase Functions emulator not running my changes in Typescript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM