使用Firebase Emulator在XCode中测试Firestore时,如何设置测试数据? [英] How to setup test data when testing Firestore in XCode with Firebase Emulator?
问题描述
几天来我一直在尝试使用测试数据设置Firebase Emulator.
I have been trying to setup Firebase Emulator with test data for days.
- 我已经成功部署了Firebase模拟器
- 我编写了一个测试,该测试检查了模拟的Firestore中的值,并且测试也通过了.
func test_WhenPartyNameIsUnique_ThenErrorIsNil() {
// given
let exp = self.expectation(description: "Waiting for Firebase Emulator")
// when
self.sut.isUnique(partyName: "akshit") { (error) in
// then
XCTAssertNil(error)
exp.fulfill()
}
self.waitForExpectations(timeout: 1, handler: nil)
}
出什么问题了?
我无法弄清楚如何为每个测试设置不同的测试数据.对于以上测试,我想检查partyName是否唯一(意味着数据库中已经存在).我该怎么办?
What's the problem?
I am not able to figure out how would I set up different test data for each of the tests. For the above test, I want to check if the partyName is not unique (meaning already present in the database). How do I do that?
推荐答案
Firestore模拟器现在支持导入/导出.因此,创建一个测试用例:
The Firestore emulator now supports import/export. So to create the a test case:
- 运行模拟器(
firebase emulators:start
) - 连接您的应用程序或脚本并创建所需的测试数据
- 在另一个终端窗口中运行
firebase emulators:export ./some-directory
现在,您将在 ./some-directory
中拥有Firestore模拟器数据的快照.您可以通过运行以下数据来启动仿真器:
Now you will have a snapshot of Firestore emulator data in ./some-directory
. You can start the emulators with that data by running:
firebase emulators:start --import=./some-directory
目前,该功能仅支持Firestore,但我们希望将来可以使用相同的流程将导入/导出添加到更多仿真器中.
Right now this only supports Firestore but we hope to add import/export to more emulators using the same flow in the future.
这篇关于使用Firebase Emulator在XCode中测试Firestore时,如何设置测试数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!