I am writing a Flutter app that has fairly complex logic over the Firebase Firestore documents.
I am trying to write unit tests using flutter_test that actually execute this logic against the database (I know this is technically an integration test). This is because this logic has a lot of edge-cases I can only be sure are working if tested against the real database.
This seems to be an impossible task.
- The cloud_firestore package can only run inside the phone because of how the authentication was implemented.
- The firebase package has 2 "implementations". One can only work on the browser, and the other one, the Dart VM one, which is a low level wrapper around the REST API, is almost completely undocumented.
The 2. firebase package for Dart VM has this example:
import 'package:firebase/firebase_io.dart';
void main() {
var credential = ... // Retrieve auth credential
var fbClient = new FirebaseClient(credential); // FirebaseClient.anonymous() is also available
var path = ... // Full path to your database location with .json appended
// GET
var response = await fbClient.get(path);
// DELETE
await fbClient.delete(path);
...
}
... however it does not show how to get the credential. The googleapis package shows how to get the credentials:
final _credentials = new ServiceAccountCredentials.fromJson(r'''
{
"private_key_id": ...,
"private_key": ...,
"client_email": ...,
"client_id": ...,
"type": "service_account"
}
''');
... however this object is not a string and it is not written anywhere how to transform this into what the FirebaseClient class expects (toString() does not work). There is a Github issue on the firebase package on how to get this credentials but it is still unanswered.
I find it hard to believe that there is no information available online, that I could find, on how to write proper integration tests for Firebase Firestore.
Considerations:
- I have no interest mocking Firestore because my logic is complex and I want to test each edge-case against the real database.
- I have no interest using
Flutter Driverbecause the tests are installed like normal apps in the phone and that takes time during development and it is not as straight-forward to debug as regular unit tests. Android Studio has very good test debug tools.
How should I write integration tests that access Firebase Firestore?