【问题标题】:How to do integration tests on Firebase Functions using local emulator?如何使用本地模拟器对 Firebase Functions 进行集成测试?
【发布时间】:2021-03-11 13:12:15
【问题描述】:

我成功地为我的 Firebase 函数编写了一些测试,但是现在我想测试操纵 Firestore 数据的函数。

为此,我执行以下操作

export GOOGLE_APPLICATION_CREDENTIALS="my-project-key.json"
export FIRESTORE_EMULATOR_HOST="localhost:8080"
firebase emulators:start --import ./functions/test/fixture --project my-project

然后我跑

npm run test

测试代码如下:

const test = require("firebase-functions-test")()
const functions = require("../index")

describe("Tests", () => {

  it("Do test", async () => {
    const wrapped = test.wrap(functions.doTest)
    const result = await wrapped({id:"1"})
  })
})

导入的index 文件包含:

const functions = require("firebase-functions")
const admin = require("firebase-admin")

admin.initializeApp()

exports.doTest = functions.https.onCall(async (data) => {
  const {id} = data
  const vehicleRef = admin.firestore().collection("vehicles").doc(id)
  const vehicle = await vehicleRef.get()
})


然而,每次我调用访问 Firestore 的 Firebase 函数时,我都会收到以下错误(在本例中为 vehicleRef.get()):

Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
    at GoogleAuth.getApplicationDefaultAsync (/MyProject/firebase/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:157:19)
    at processTicksAndRejections (internal/process/task_queues.js:94:5)
    at async GoogleAuth.getClient (/MyProject/firebase/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:490:17)
    at async GrpcClient._getCredentials (/MyProject/firebase/functions/node_modules/google-gax/build/src/grpc.js:87:24)
    at async GrpcClient.createStub (/MyProject/firebase/functions/node_modules/google-gax/build/src/grpc.js:212:23)
Caused by: Error
    at Firestore.getAll (/MyProject/firebase/functions/node_modules/@google-cloud/firestore/build/src/index.js:784:23)
    at DocumentReference.get (/MyProject/firebase/functions/node_modules/@google-cloud/firestore/build/src/reference.js:201:32)
    at Function.run (/MyProject/firebase/functions/src/tracker.js:40:28)
    at wrapped (/MyProject/firebase/functions/node_modules/firebase-functions-test/lib/main.js:72:30)
    at Context.<anonymous> (/MyProject/firebase/functions/test/testTracker.js:42:26)
    at callFn (/MyProject/firebase/functions/node_modules/mocha/lib/runnable.js:366:21)
    at Test.Runnable.run (/MyProject/firebase/functions/node_modules/mocha/lib/runnable.js:354:5)
    at Runner.runTest (/MyProject/firebase/functions/node_modules/mocha/lib/runner.js:677:10)
    at /MyProject/firebase/functions/node_modules/mocha/lib/runner.js:801:12
    at next (/MyProject/firebase/functions/node_modules/mocha/lib/runner.js:594:14)
    at /MyProject/firebase/functions/node_modules/mocha/lib/runner.js:604:7
    at next (/MyProject/firebase/functions/node_modules/mocha/lib/runner.js:486:14)
    at Immediate._onImmediate (/MyProject/firebase/functions/node_modules/mocha/lib/runner.js:572:5)

我做错了什么?

【问题讨论】:

    标签: firebase google-cloud-firestore


    【解决方案1】:

    看起来你做的一切都是正确的,除了我认为你没有正确初始化 initializeApp 对象。在您的index 中试试这个:

    admin.initializeApp({
      projectId: 'my-project',
      credential: admin.credential.applicationDefault(),
    });
    

    您必须根据是否设置环境变量来添加使用此版本的条件。

    【讨论】:

    • @JorisMans,这能回答你的问题吗?
    猜你喜欢
    • 1970-01-01
    • 2017-01-23
    • 2020-07-28
    • 2019-05-21
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    • 1970-01-01
    • 2019-02-23
    相关资源
    最近更新 更多