Firebase 功能测试 "The default Firebase app does not exist."

标签 firebase firebase-realtime-database google-cloud-functions google-cloud-firestore

我已经开始使用新的 firebase-functions-test测试 SDK 以对我的 进行单元测试云火存储 职能。当我通过 npm test 运行测试时我收到以下错误:
The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services
我的方法与 makeUppercase 非常相似在 quickstarts samples 中找到的方法,因此我主要从 test.js 复制测试文件。

describe('updateActiveNotesCount', () => {

    // Test Case: Setting environments/{environment}/users/{userId}/people/{personId}/notes/{noteId}/isActive to 'true' should cause
    // .../{personId}/activeNotesCount to be incremented

    it('Should increment active notes count on the person field', () => {
      // [START assertOffline]
      const childParam = 'activeNotesCount'; // The field to set
      const setParam = '1'; // the new activeNotesCount

      const childStub = sinon.stub();
      const setStub = sinon.stub();

      // A fake snap to trigger the function
      const snap = {
        // I believe this represents event params, wildcards in my case
        params: {
          userId: () => '1',
          personId: () => '2',
          environment: () => 'dev',  
        },
        // Not ENTIRELY sure how to write this one out
        ref: {
          parent: {
            child: childStub
          }
        }
      };
      childStub.withArgs(childParam).returns({ set: setStub });
      setStub.withArgs(setParam).returns(true);

      const wrapped = test.wrap(myFunctions.updateActiveNotesCount);

      return assert.equal(wrapped(snap), true);
      // [END assertOffline]
    })
  });

我无法摆脱这个错误。

编辑:
我看到他们现在更新了文档以包含新的 SDK,并且他们提到必须模拟配置值。在我的 index.js , 我用:
const config = functions.config().firebase
admin.initializeApp(config);

我现在尝试像这样模拟它:
before(() => {
    // [START stubAdminInit]
    test.mockConfig({ firebase: 'FakeId' });

    adminInitStub = sinon.stub(admin, 'initializeApp');

    myFunctions = require('../index');
    // [END stubAdminInit]
  });

但没有运气。

结束编辑

对此的任何帮助将不胜感激。

最佳答案

这对我有用:

关键点:您必须在测试框架内显式初始化应用程序,然后将其 stub

const chai = require('chai');
const assert = chai.assert;
const sinon = require('sinon');
const admin = require('firebase-admin');
const functions = require('firebase-functions');
const test = require('firebase-functions-test')();


describe('Functions', () => {
  let _fns, adminInitStub;

  before(() => {
    test.mockConfig({ YOUR_CONFIG }); 
    admin.initializeApp(functions.config().firebase);
    adminInitStub = sinon.stub(admin, "initializeApp");
    _fns = require('../index');
  });

  after(() => {
    test.cleanup();
  });

  describe('when test case blah', () => {
    // Test Case: setting messages/{pushId}/original to 'input' should cause 'INPUT' to be written to
    // messages/{pushId}/uppercase
    it('should do xyz', () => {
      assert(1 + 1, 2);
    });
  });
});

关于Firebase 功能测试 "The default Firebase app does not exist.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49661782/

相关文章:

javascript - firebase函数onwrite的值有时为null

google-cloud-platform - 为什么对 Cloud Functions 的身份验证与 Cloud Endpoints 不同?

android - 如何使用 firebase 查询过滤 android listview 项目?

java - 如何从 Firebase Android 中的用户 ID 获取用户对象

javascript - 火力地堡 : Wait For Async and Push to an array

android - FirebaseError 未解决

javascript - Firebase Firestore - 异步/等待在继续之前不等待获取数据?

ios - 使用 Firebase 保持用户登录(自定义 iOS 身份验证)

javascript - 如何使用 Cloud Functions 在 Firebase 中解决 "TypeError: Cannot read property ' firstname' of undefined"?

Firebase - 云函数 : Always running functions