How to setup test data when testing Firestore in XCode with Firebase Emulator?

放肆的年华 提交于 2021-01-28 19:16:15

问题


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:

  1. Run the emulators (firebase emulators:start)
  2. Connect your app or a script and create the test data you want
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!