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

Posted

技术标签:

【中文标题】Firebase-functions-test “默认的 Firebase 应用不存在。”【英文标题】:Firebase-functions-test "The default Firebase app does not exist." 【发布时间】:2018-09-14 15:38:16 【问题描述】:

我已经开始使用新的 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 samples 中的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]
    )
  );

我无法摆脱这个错误。

编辑: 我看到他们现在更新了文档以包含新的 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]
  );

但没有运气。

结束编辑

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

【问题讨论】:

initializeApp() 放在config 对象之后,并确保它们位于脚本的顶部。 由于我正在尝试进行离线(模拟)测试,我不确定是否有必要。在上面的示例中,他们没有创建配置对象,但在 online example 中他们这样做了 参见第 24 行 const sinon = require('sinon'); 然后第 42 行 adminInitStub = sinon.stub(admin, 'initializeApp');。不知道'sinon'是什么或在哪里...... 【参考方案1】:

这对我有用:

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

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);
    );
  );
);

【讨论】:

【参考方案2】:

https://github.com/firebase/firebase-functions-test/issues/38

我的解决方案...

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

// TESTS
describe('Cloud Functions', () => 
  let myFunctions, adminInitStub;

  before(() => 
    // ORDER REQUIRED
    // 1) Calling 'exampleDocumentSnapshot' only way to initialize testing app
    test.firestore.exampleDocumentSnapshot()

    // 2) Prevents multiple initializeApp calls
    adminInitStub = sinon.stub(admin, 'initializeApp');

    // 3) Import functions
    myFunctions = require('../');
  );

【讨论】:

【参考方案3】:

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

所以,不要存根 configfirebaseinitializeApp,只需这样做:

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

describe('Cloud Functions', () => 
    admin.initializeApp(functions.config().firebase);
    before(() => 
        const index = require('../index');

【讨论】:

以上是关于Firebase-functions-test “默认的 Firebase 应用不存在。”的主要内容,如果未能解决你的问题,请参考以下文章