带有关于 pubsub 主题的消息的 Firebase 模拟器不会触发云功能

时间:2021-04-18 16:02:50

标签: firebase google-cloud-functions integration-testing google-cloud-pubsub firebase-tools

我正在开发一个普通的快速支持 API 项目,该项目使用 Firebase 函数进行一些离线数据处理。 在许多情况下,API 将事件引发到 pub-sub,然后由云函数获取。 一个典型的例子是所有的聚合(天/月的总销售额)都被推送到云函数。

我正在寻找使用 firebase 模拟器的集成测试套件,我可以在其中从 API 层进行测试,直到触发云功能 及其对数据存储的更新。在模拟器上拥有集成测试套件为我提供了速度和成本以及快速本地验证方面的良好优势。

我正在使用 firebase 模拟器调用我的常规测试脚本

firebase emulators:exec "npm run test"

在运行测试时,我可以初始化所有发布订阅功能。

enter image description here

随后我可以看到该消息也被推送到了主题。

enter image description here

但是我没有看到相应的订阅 firebase 函数正在执行。我的 firebase 函数骨架就像

export const addIncentiveData = functions
    .region("asia-south1")
    .pubsub.topic("on_wf_data_added")
    .onPublish(async (message, context) => {
        const FUNCTION_NAME = `addIncentiveData`;
        console.log(`[${FUNCTION_NAME}] Entering `)
        const FUNCTION_NAME = `addIncentiveData`;
        logger.info(`[${FUNCTION_NAME}] : The function was triggered at ${context.timestamp}`);
        logger.debug(`[${FUNCTION_NAME}] : The message is ${JSON.stringify(message)}`);

        const payLoad = JSON.parse(Buffer.from(message.data, "base64").toString());
        logger.debug(`[${FUNCTION_NAME}] : The payload is ${JSON.stringify(payLoad)}`);
        
        //some processing

        logger.info(`[${FUNCTION_NAME}] : Completed processing`);
        return;
    });

1 个答案:

答案 0 :(得分:1)

问题出在我的配置上。 mocha 测试调用 API,API 将消息推送到真正的 pubsub 主题。我必须输入变量才能让我的 API 知道与模拟器一起工作。

我将以下变量添加到我的 .env

PUBSUB_EMULATOR_HOST=localhost:8085
PUBSUB_PROJECT_ID=my-project

随后我可以看到在模拟器运行时所有消息都被推送到模拟器。

来源:Firebase PubSub Emulator not recieving messages