我正在编写 firebase 存储安全规则,并使用“@firebase/rules-unit-testing”对它们进行单元测试。运行测试时,出现以下错误:
Not Implemented
at node_modules/@firebase/rules-unit-testing/src/impl/rules.ts:64:11
at step (node_modules/@firebase/rules-unit-testing/dist/index.cjs.js:74:23)
at Object.next (node_modules/@firebase/rules-unit-testing/dist/index.cjs.js:55:53)
at fulfilled (node_modules/@firebase/rules-unit-testing/dist/index.cjs.js:45:58)
at tryCallOne (node_modules/promise/lib/core.js:37:12)
at node_modules/promise/lib/core.js:123:15
at flush (node_modules/asap/raw.js:50:29)
我目前唯一的单元测试应该是确认用户不能删除文件。
这是我的代码:
import {
assertFails,
assertSucceeds,
initializeTestEnvironment,
RulesTestEnvironment,
} from "@firebase/rules-unit-testing";
import {
deleteObject,
ref,
uploadBytes,
} from "firebase/storage";
import * as fs from "fs";
const PROJECT_ID = "iurn-973d0";
let testEnv: RulesTestEnvironment;
describe("Firebase storage security rules", () => {
beforeAll(async () => {
testEnv = await initializeTestEnvironment({
projectId: PROJECT_ID,
firestore: {
rules: fs.readFileSync(__dirname + "/../storage.rules", "utf-8"),
host: "127.0.0.1",
port: 9199,
},
});
});
beforeEach(async () => {
if(!testEnv) return
testEnv.clearStorage();
});
it("DOES NOT allow deletion", async () => {
if(!testEnv) return
const storage = testEnv.authenticatedContext("user123").storage();
await testEnv.withSecurityRulesDisabled(async (context) => {
await uploadBytes(ref(context.storage(), "hi.jpg"), require("../assets/images/placeholder.png"))
})
expect(assertFails(deleteObject(ref(storage, "hi.jpg")))).resolves.toBeTruthy()
})
afterAll(async () => {
if(!testEnv) return
testEnv.cleanup();
});
});
“未实施”错误很可能是由于“@firebase/rules-unit-testing”库与您正在使用的 Firebase SDK 之间的版本不匹配引起的。
确保您使用的是与您的 Firebase SDK 版本兼容的最新版本的“@firebase/rules-unit-testing”。您可以在“@firebase/rules-unit-testing”文档中查看兼容性矩阵。
如果您已经确定版本兼容,请尝试将您的 Firebase SDK 和“@firebase/rules-unit-testing”库更新到最新版本。
此外,您可以尝试在代码中添加一些调试日志记录,以查看错误发生的位置。你也可以尝试在单元测试环境之外运行你的代码,看看你是否得到同样的错误。