Replies: 6 comments 1 reply
-
UPDATE I just noticed that the final firebaseAuthProvider = Provider<FirebaseAuth>((ref) {
return FirebaseAuth.instance;
});
class CustomSignInScreen extends ConsumerWidget {
const CustomSignInScreen({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
return SignInScreen(
auth: ref.watch(firebaseAuthProvider),
providers: [EmailAuthProvider()],
);
}
} Then, I can do this in the tests: import 'package:faker_app_flutter_firebase/src/routing/app_router.dart';
import 'package:faker_app_flutter_firebase/src/screens/custom_sign_in_screen.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
class MockFirebaseAuth extends Mock implements FirebaseAuth {}
class MockFirebaseApp extends Mock implements FirebaseApp {}
void main() async {
testWidgets('custom sign in screen ...', (tester) async {
final mockAuth = MockFirebaseAuth();
final mockFirebaseApp = MockFirebaseApp();
when(() => mockAuth.app).thenReturn(mockFirebaseApp);
await tester.pumpWidget(
ProviderScope(
overrides: [
firebaseAuthProvider.overrideWithValue(mockAuth),
],
child: const MaterialApp(
home: CustomSignInScreen(),
),
),
);
});
} This takes care of injecting the However, running the test causes this exception:
I've also tried using the Overall, it would be good to have some guidance/documentation/examples showing how to write widget and integration tests with Firebase UI. Something along the lines of this guide, but updated for Firebase UI would be great: |
Beta Was this translation helpful? Give feedback.
-
final firebaseAuthProvider = Provider((ref) { |
Beta Was this translation helpful? Give feedback.
-
No guide to being able to setup a mock here is quite annoying. |
Beta Was this translation helpful? Give feedback.
-
Ah.. found it. In your test setup call this.
If you look at the call stack from the error, configureProviders(...) will check if it's in test mode before throwing wrt initializing. |
Beta Was this translation helpful? Give feedback.
-
When using Firebase UI, how can we mock the underlying Firebase SDK so we can run widget and integration tests that don't talk with the underlying Firebase backend?
This is easy enough to do for projects that don't use the Firebase UI. For example, apps that use Riverpod can do this:
And this in the tests:
But I can't see any way to override theFigured this out but still stuck when running the tests, see my comment below.FirebaseAuth
instance that is used internally by Firebase UI (or at least it's not documented).Ideally there should be a way to still use mocks (whether from packages like mocktail or the Firebase-specific mocks mentioned in this comment).
Any advice?
Beta Was this translation helpful? Give feedback.
All reactions