Firebase-functions-test “默认的 Firebase 应用不存在。”

Bra*_*don 5 firebase firebase-realtime-database google-cloud-functions google-cloud-firestore

我已经开始使用新的firebase-functions-test测试 SDK 来对我的Cloud Firestore功能进行单元测试。当我通过运行测试npm test时,出现以下错误:

The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services

我的方法与quickstarts示例中makeUppercase方法非常相似,因此我主要从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]
    })
  });
Run Code Online (Sandbox Code Playgroud)

我无法克服这个错误。

编辑: 我看到他们现在更新了文档以包含新的 SDK,并且他们提到必须模拟配置值。在我的index.js,我使用:

const config = functions.config().firebase
admin.initializeApp(config);
Run Code Online (Sandbox Code Playgroud)

我现在试着像这样嘲笑它:

before(() => {
    // [START stubAdminInit]
    test.mockConfig({ firebase: 'FakeId' });

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

    myFunctions = require('../index');
    // [END stubAdminInit]
  });
Run Code Online (Sandbox Code Playgroud)

但没有运气。

结束编辑

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

Ali*_*Nem 7

我遇到了同样的问题,无论我如何尝试,我都无法存根 initializeApp。我发现的解决方法是admin.initializeApp(functions.config().firebase);在 TEST 文件的开头调用。

所以,与其磕碰configfirebaseinitializeApp,只是这样做:

const admin = require('firebase-admin');
const functions = require('firebase-functions');

describe('Cloud Functions', () => {
    admin.initializeApp(functions.config().firebase);
    before(() => {
        const index = require('../index');
Run Code Online (Sandbox Code Playgroud)