Firebase功能单元测试错误

时间:2019-02-15 15:54:41

标签: typescript firebase firebase-authentication google-cloud-firestore google-cloud-functions

我正在尝试对HTTP onRequest调用运行一些单元测试。但是,初始化firebase以同时连接到authfirestore时存在一些问题(我的API调用同时使用了两者)。

下面是一个示例getObjectNames.spec.ts

// Dependencies
// ------------

// Libraries
import { expect } from 'chai';
import { stub } from 'sinon';

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

// Tests
// -----

describe('src/firestore/apis/getObjectNames', () => {
    let adminInitStub : any;
    let moduleExports : any;

    before(() => {
        test.mockConfig({ firebase: 'TestId' });
        adminInitStub = stub(admin, 'initializeApp');
        moduleExports = require('../../../../src/firestore/apis/getObjectNames');
    });

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

    after(() => {
        adminInitStub.restore();
    });

    describe('module exports', () => {
        it('exports correctly', () => {
            expect(moduleExports).to.have.property('getObjectNames');
        });
    });

    describe('HTTP requests', () => {
        it('handles GET request correctly', () => {
            const req = {
                get: (val: string) => {
                    switch(val) {
                        case 'Authorization':
                            return 'Bearer IRRyyrWMTEUYP9jK4It5ta0Qtpp2';
                        default:
                            return 'foo';
                    }
                },
                headers: {
                    origins: true
                },
                method: 'GET'
            };
            const res = {
                getHeader: stub(),
                setHeader: stub(),
                status: (code: number) => {
                    expect(code).to.equal(200);
                }
            };

            moduleExports.getObjectNames(req, res);
        });
    });
});

我在本地运行函数getObjectNames并且可以运行,但是尝试运行此测试文件会出现以下错误:

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

我尝试遵循this GitHub issues post,但甚至添加了

try {
  admin.initializeApp();
} catch (e) {}

功能顶部似乎无济于事。

0 个答案:

没有答案
相关问题