Tutorial how to test with firebase #5550
Replies: 8 comments 3 replies
-
The issue at flutter/flutter#15245 has been closed and moved here. Future collaboration on this issue will be done here. |
Beta Was this translation helpful? Give feedback.
-
I have an authentication package that provides mocks for platform channels to allow running unit tests using It provides essentials like logging in, logging out, creating an account, requesting a password reset email. firebase_auth, google_sign_in and facebook_login plugins are supported. Not all messages are (yet) supported, nor are all authentication methods available through firebase, though I am happy to help add such functionality to the package if there is interest. The meat of what is needed to support testing is done in the MockXXXChannel sources in the package.
The source code can be found here: https://gitlab.com/playscale-free/authenticator There is also a similar package for firebase storage in the works that follows the same pattern. |
Beta Was this translation helpful? Give feedback.
-
I've published mocks for Firestore, Firebase Storage and Firebase Auth (with Google Sign In) at:
Here's how to test that Firestore's demo app renders messages using cloud_firestore_mocks: import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:cloud_firestore_mocks/cloud_firestore_mocks.dart';
import 'package:firestore_example/main.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('shows messages', (WidgetTester tester) async {
// Populate the mock database.
final firestore = MockFirestoreInstance();
await firestore.collection('messages').add({
'message': 'Hello world!',
'created_at': FieldValue.serverTimestamp(),
});
// Render the widget.
await tester.pumpWidget(MaterialApp(
title: 'Firestore Example', home: MyHomePage(firestore: firestore)));
// Let the snapshots stream fire a snapshot.
await tester.idle();
// Re-render.
await tester.pump();
// // Verify the output.
expect(find.text('Hello world!'), findsOneWidget);
expect(find.text('Message 1 of 1'), findsOneWidget);
});
} You can find the full example at https://github.com/atn832/cloud_firestore_mocks/tree/master/example. |
Beta Was this translation helpful? Give feedback.
-
@atn832 I find it really difficult to test SignInWithEmailAndPassword using your Mock classes, I think it is because AuthResult that I don't know how to mock it and get a valid result and then a valid user after, can you please provide an illustration of that ? |
Beta Was this translation helpful? Give feedback.
-
So far firebase_auth_mocks only supports |
Beta Was this translation helpful? Give feedback.
-
I can not wait to see your work, thank you for your help. |
Beta Was this translation helpful? Give feedback.
-
I've implemented it and just published an update. In the future, feel free to post issues directly to the relevant repository so that people subscribed to this thread don't get alerts: |
Beta Was this translation helpful? Give feedback.
-
How about setting initial data for each individual test suite when running integration tests with the emulator ? There is no admin SDK so this seems a bit more complicated than I anticipated, especially if your rules setup does not allow you to clear some data directly from the tests. The only thing I can think of is to somehow load an emulator without the security rules and test the security rules separately but that's contrived. |
Beta Was this translation helpful? Give feedback.
-
Hi this is a feature request not an issue.
Right now the Widget test secion of https://flutter.io/testing/ is kinda poor and does not give developers better examples of how to "mock" integration services.
Being firebase one of the most used altogether with flutter it would be nice to have a Firebase testing related tutorial/sample.
I know this is kind of testing101 with dart, but must of us (this is my personal belief) are coming to flutter without any dart knowledge.
A clear example of this (and what I'm currently battling) is how to test components that rely on
FirebaseAnimatedList
(Sorry if the issue does not follow the guidelines, I looked for feature requests and couldnt find)
Beta Was this translation helpful? Give feedback.
All reactions