要为允许您模拟读/写和设置测试数据的云功能设置测试环境,您必须执行以下操作。请记住,这确实模拟/触发了云功能。因此,在写入 firestore 后,您需要稍等片刻,直到云函数完成写入/处理,然后才能读取断言数据。
可以在此处找到包含以下代码的示例 repo:https://github.com/BrandiATMuhkuh/jaipuna-42-firebase-emulator。
前提条件
我假设此时您已经设置了一个 firebase 项目,其中包含一个函数文件夹和 index.js。测试稍后将位于functions/test 文件夹中。如果您没有项目设置,请使用firebase init 设置项目。
安装依赖项
首先将以下依赖项添加/安装:mocha、@firebase/rules-unit-testing、firebase-functions-test、firebase-functions、firebase-admin、firebase-tools 到 functions/package.json 而不是根文件夹。
cd "YOUR-LOCAL-EMULATOR"/functions (for example cd C:\Users\User\Documents\FirebaseLocal\functions)
npm install --save-dev mocha
npm install --save-dev firebase-functions-test
npm install --save-dev @firebase/rules-unit-testing
npm install firebase-admin
npm install firebase-tools
替换所有jaipuna-42-firebase-emulator 名称
使用自己的project-id 非常重要。它必须是您自己项目的project-id,并且必须存在。假身份证是不行的。因此,在下面的代码中搜索所有 jaipuna-42-firebase-emulator 并将其替换为您的 project-id。
index.js 示例云函数
// functions/index.js
const functions = require("firebase-functions");
const admin = require("firebase-admin");
// init the database
admin.initializeApp(functions.config().firebase);
let fsDB = admin.firestore();
const heartOfGoldRef = admin
.firestore()
.collection("spaceShip")
.doc("Heart-of-Gold");
exports.addCrewMemeber = functions.firestore.document("characters/{characterId}").onCreate(async (snap, context) => {
console.log("characters", snap.id);
// before doing anything we need to make sure no other cloud function worked on the assignment already
// don't forget, cloud functions promise an "at least once" approache. So it could be multiple
// cloud functions work on it. (FYI: this is called "idempotent")
return fsDB.runTransaction(async t => {
// Let's load the current character and the ship
const [characterSnap, shipSnap] = await t.getAll(snap.ref, heartOfGoldRef);
// Let's get the data
const character = characterSnap.data();
const ship = shipSnap.data();
// set the crew members and count
ship.crew = [...ship.crew, context.params.characterId];
ship.crewCount = ship.crewCount + 1;
// update character space status
character.inSpace = true;
// let's save to the DB
await Promise.all([t.set(snap.ref, character), t.set(heartOfGoldRef, ship)]);
});
});
mocha 测试文件 index.test.js
// functions/test/index.test.js
// START with: yarn firebase emulators:exec "yarn test --exit"
// important, project ID must be the same as we currently test
// At the top of test/index.test.js
require("firebase-functions-test")();
const assert = require("assert");
const firebase = require("@firebase/testing");
// must be the same as the project ID of the current firebase project.
// I belive this is mostly because the AUTH system still has to connect to firebase (googles servers)
const projectId = "jaipuna-42-firebase-emulator";
const admin = firebase.initializeAdminApp({ projectId });
beforeEach(async function() {
this.timeout(0);
await firebase.clearFirestoreData({ projectId });
});
async function snooz(time = 3000) {
return new Promise(resolve => {
setTimeout(e => {
resolve();
}, time);
});
}
it("Add Crew Members", async function() {
this.timeout(0);
const heartOfGold = admin
.firestore()
.collection("spaceShip")
.doc("Heart-of-Gold");
const trillianRef = admin
.firestore()
.collection("characters")
.doc("Trillian");
// init crew members of the Heart of Gold
await heartOfGold.set({
crew: [],
crewCount: 0,
});
// save the character Trillian to the DB
const trillianData = { name: "Trillian", inSpace: false };
await trillianRef.set(trillianData);
// wait until the CF is done.
await snooz();
// check if the crew size has change
const heart = await heartOfGold.get();
const trillian = await trillianRef.get();
console.log("heart", heart.data());
console.log("trillian", trillian.data());
// at this point the Heart of Gold has one crew member and trillian is in space
assert.deepStrictEqual(heart.data().crewCount, 1, "Crew Members");
assert.deepStrictEqual(trillian.data().inSpace, true, "In Space");
});
运行测试
要一次性运行测试和模拟器,我们导航到functions 文件夹并写入yarn firebase emulators:exec "yarn test --exit"。此命令也可以在您的 CI 管道中使用。或者你也可以改用npm test。
如果一切正常,您应该会看到以下输出
√ Add Crew Members (5413ms)
1 passing (8S)