Unit Testing firebase_auth and firestore - firebase

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:
A month of Flutter: mocking Firebase Auth in tests
Setup Unit test for Firebase User authentication with Mockito
How to create integration test for app with Firebase Auth?
Flutter Testing [4] - Firebase Auth App - Unit Testing
Mocking In Integration Tests with Flutter
Writing Unit Tests in Flutter with Firebase Firestore
Tutorial how to test with firebase #15245

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

Calling cypress-firebase's cy.login() does not result in logged-in user

I am using Cypress for end-to-end testing an application, together with the cypress-firebase package. I followed the official setup instructions (using TypeScript, and the Firebase Web SDK version 9 with the compat mode).
In my tests, calling cy.login() seems to work as expected in that Cypress logs that the createCustomToken task successfully performs two HTTP POSTs with status code 200 each.
However, when visiting a page of the application afterwards, no user is logged in. In the application, the login status is supposed to be detected using Firebase's onAuthStateChanged function. It seems that cy.login() never triggers onAuthStateChanged, though.
My best guess is that maybe the auth instances used by cypress-firebase and the application code are not the same?! Is there a way to confirm this, or could there be another reason?
Happy to provide further information if it may help.
The issue was indeed related to auth instances not matching: The application was using a named instance, whereas the initializeApp code in Cypress was not naming the app (and thus relying in an app instance named [DEFAULT]).
Fortunately, cypress-firebase has support for named apps:
const namedApp = firebase.initializeApp(fbConfig, "app_name");
attachCustomCommands({ Cypress, cy, firebase, app: namedApp });

Querying Firebase A/B testing using API

I'm writing a script that will generate extended A/B testing results. Is it possible to query Firebase A/B testing using API to get an experiment variant list, start date, end date, etc?
There currently is no API to get/manage the list of experiments for Firebase A/B testing. All management of experiments must be done through the Firebase console at the moment.
You might want to file a feature request, and keep an eye out on the Firebase blog and release notes for changes.

How to let a locally-emulated Firebase Function hit real remote Firestore?

Due to there still being no support for web client reads from locally-emulated firestore at the moment, my best option for some parts of development is to allow the local functions to modify the real Firestore (in a separate dev project).
I tried the "Admin SDK configuration snippet", but it doesn't work, the reason I suppose being the mocked firebase-admin.
Has anyone documented doing this properly?

maintain connection status of user with firestore. using firebase db and cloud function

I am trying to maintain connection status of user with firestore . but i don't understand some points.
Link : https://firebase.google.com/docs/firestore/solutions/presence
i refer the above link description but i don't understand how to write code.
see the below given topics:
Using presence in Realtime Database
where to write this given code in application or Cloud functions?
2.Connecting to Cloud Firestore
Updating Cloud Firestore's local cache
where to write local cache update code?
I use this in my android application.
Each section of the code in the Build Presence in Cloud Firestore is marked.
If it is marked with WEB, you'll need to run this code in your web application. If your clients are native Android or iOS, you'll need to write and run similar code for those platforms.
If the code is marked with NODE.JS, you will need to run this code in a Node.js environment. It should be possible to accomplish the same in Cloud Functions, but no sample is provided for that.

How to run tests with code that depends on plugins?

I want to test my Firebase data access layer with direct access to Firebase.
Is flutter drive the only way to run such tests, or are there other ways like running in emulator using flutter test?
What are the possible approaches and how can I set them up?
I'm not aware of a way to talk to a real database with flutter test currently, because there's no Firebase SDK to do the talking to the network. You probably wouldn't want this anyway, because it could make your tests nondeterministic or flaky if the data changes or the Firebase servers aren't accessible.
Some plugins, like shared_preferences, have an API for providing mock values. You could do something like that for the firebase_database plugin, either as a pull request to the first-party repo or by calling MethodChannel.setMockMethodCallHandler and BinaryMessages.handlePlatformMessage in your test code to simulate what the native side would be doing.

Resources