如何使用本地模拟器对Firebase函数进行集成测试?

如何使用本地模拟器对Firebase函数进行集成测试?,firebase,google-cloud-firestore,Firebase,Google Cloud Firestore,我成功地为我的Firebase函数编写了一些测试,但是现在我想测试操纵Firestore数据的函数 为此,我执行以下命令 export GOOGLE_APPLICATION_CREDENTIALS="my-project-key.json" export FIRESTORE_EMULATOR_HOST="localhost:8080" firebase emulators:start --import ./functions/test/fixture --

我成功地为我的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运行测试

测试代码如下所示:

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"})
  })
})
导入的
索引
文件包含:

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函数时,我都会收到以下错误(在本例中,是在
VehiclerRef.get()
)上):

错误:无法加载默认凭据。浏览到https://cloud.google.com/docs/authentication/getting-started 了解更多信息。
位于GoogleAuth.getApplicationDefaultAsync(/MyProject/firebase/functions/node_modules/google auth library/build/src/auth/GoogleAuth.js:157:19)
在处理和拒绝时(内部/process/task_queues.js:94:5)
异步GoogleAuth.getClient(/MyProject/firebase/functions/node_modules/google auth library/build/src/auth/GoogleAuth.js:490:17)
异步GrpcClient.\u获取凭据(/MyProject/firebase/functions/node\u modules/google gax/build/src/grpc.js:87:24)
异步GrpcClient.createStub(/MyProject/firebase/functions/node_modules/google gax/build/src/grpc.js:212:23)
原因:错误
在Firestore.getAll(/MyProject/firebase/functions/node_modules/@google cloud/Firestore/build/src/index.js:784:23)
在DocumentReference.get(/MyProject/firebase/functions/node_modules/@googlecloud/firestore/build/src/reference.js:201:32)
在Function.run(/MyProject/firebase/functions/src/tracker.js:40:28)
包装时(/MyProject/firebase/functions/node_modules/firebase functions test/lib/main.js:72:30)
在上下文中。(/MyProject/firebase/functions/test/testTracker.js:42:26)
在callFn(/MyProject/firebase/functions/node_modules/mocha/lib/runnable.js:366:21)
在Test.Runnable.run(/MyProject/firebase/functions/node_modules/mocha/lib/Runnable.js:354:5)
在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
接下来(/MyProject/firebase/functions/node_modules/mocha/lib/runner.js:594:14)
at/MyProject/firebase/functions/node_modules/mocha/lib/runner.js:604:7
接下来(/MyProject/firebase/functions/node_modules/mocha/lib/runner.js:486:14)
立即(/MyProject/firebase/functions/node\u modules/mocha/lib/runner.js:572:5)

我做错了什么?

看起来你做的一切都对,只是我认为你没有正确初始化
initializeApp
对象。在您的
索引中尝试以下操作:

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

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

@JorisMans,这是否回答了您的问题?
admin.initializeApp({
  projectId: 'my-project',
  credential: admin.credential.applicationDefault(),
});