Unit Testing firebase_auth and firestore

Viewed 486

I am trying to setup flutter scripts for testing my classes that interface with my firestore database.

My current struggles are:

  • It seems that in order to use firebase_auth, I have to have a widget tree. I'm new to both flutter and firebase I am slowly building up my expertise by building classes one at a time, writing tests for them and then moving to the next class. Requiring the widget tree means I can't test the authorization logic independent of the GUI (e.g. What happens if I call signOut twice in my code even though the GUI logic only lets you press the button once?).
  • Mocking is insufficient. In order to mock, you need to know the expected behaviour for the tests to be representative. I've spent a lot of time trying to troubleshoot issues with futures, changing signed in states, and incorrect data formats. If I had written mocks, they wouldn't have matched how the real functions actually behave.
  • Mocking doesn't help me as I am also learning about the security rules for the database. I've definitely got code that works with lax security rules but there's something wrong with the current security rules.

Is there any way to write unit/integration tests for firebase_auth or firestore without mocking? I'm even happy using a local emulator/export of my database if that's a thing that's even possible.

Here are a number of the articles I have reviewed that are not what I am looking for:

1 Answers

According to this Github issue, it's not possible to run unit tests because native API calls are required.

Integration tests are supposed to be possible but the page from the Github Issue doesn't actually give an example of how to run the integration test.

Alright, I think I have found what I am looking for. The firebase team does a really good job showing how to test security rules in Unit testing security rules with the Firebase Emulator Suite.

What I did was I ran firebase init in my actual flutter project directory and this article Use the Firestore Emulator with Flutter shows how to access the emulator directly in flutter, however the command to initialize is now:

FirebaseFirestore.instance.useFirestoreEmulator('localhost', 8080);


Combining these, I think it should be possible to test whether or not one is writing the correct data within the correct format into the database along with ensuring the the data that's returned matches what's intended regardless of whether or not you are using Freezed, Json Serializable, dart:convert, etc...

Related