问题
I have been trying to setup Firebase Emulator with test data for days.
What I have done?
- I have successfully deployed Firebase Emulators
- I have written a test which checks for a value in the emulated Firestore and the test passes as well.
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)
}
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?
回答1:
The Firestore emulator now supports import/export. So to create the a test case:
- Run the emulators (
firebase emulators:start
) - Connect your app or a script and create the test data you want
- In another terminal window run
firebase emulators:export ./some-directory
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
Right now this only supports Firestore but we hope to add import/export to more emulators using the same flow in the future.
回答2:
The common way to get data into the emulators right now is to run code to inject that data from your application code at startup.
Work is underway on a way to inject data on startup as a command line option to the emulators, but as usual: we can't make any promises, nor give a timeline. Until that lands though, the test data will have to be inserted from your code typically in the setup of the tests.
来源:https://stackoverflow.com/questions/61463374/how-to-setup-test-data-when-testing-firestore-in-xcode-with-firebase-emulator