【问题标题】:How to unit test scheduled firebase function?如何对预定的 Firebase 功能进行单元测试?
【发布时间】:2019-05-01 16:32:20
【问题描述】:

我已经编写了一个预定函数,如下所述:https://firebase.google.com/docs/functions/schedule-functions

我现在如何测试这个功能?当我包装这个函数时(就像我对任何其他云函数一样),有一个错误说:“类型'TestFunction'上不存在属性'wrap'。”

const functionWrapper = test.wrap(function);

还有其他方法可以测试这些功能吗?

【问题讨论】:

    标签: firebase google-cloud-functions


    【解决方案1】:

    我发现的一种解决方法是将我的代码隔离在一个函数中,并从预定函数中调用该函数。我测试的时候不调用调度函数,而是直接调用隔离函数。

    例如:

    export const dailyJob = functions.pubsub
            .schedule('0 0 * * *')
            .onRun(async context => {
        return isolatedFunction();
    })
    
    export function isolatedFunction() {
        ...
    }
    

    【讨论】:

      【解决方案2】:

      你可以这样写

      exports.scheduledFunction = () => functions.pubsub.schedule('every 1 minutes').onRun((context) => {
          console.log('Start scheduledFunction every 1 minutes');
          sendEmail();
          return null;
      });
      
      
      async function sendEmail() {
          console.log('Start sendEmail');
      }
      

      【讨论】:

        【解决方案3】:
        > firebase functions:shell  
        > firebase> RUN_NAME_OF_THE_FUCTION()
        

        不知道从什么时候开始 - 但这是我处理调度程序功能的方式。我的问题是我没有上下文,也不知道如何将参数传递给这些函数。

        WIP 但至少我可以轻松地在本地环境中手动运行它。

        【讨论】:

          猜你喜欢
          • 2020-11-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-12
          • 2022-11-29
          • 1970-01-01
          • 2018-02-25
          • 2011-09-16
          • 1970-01-01
          相关资源
          最近更新 更多