A simple Flutter app demonstrating Firebase Firestore and Authentication integration with comprehensive testing using fakes.
- Task Management: Add and toggle completion of tasks
- User Authentication: Sign in/up with email/password or anonymously
- Real-time Updates: Tasks update in real-time using Firestore streams
- Comprehensive Testing: Unit and widget tests using
fake_cloud_firestoreandfirebase_auth_mocks
lib/
├── main.dart # App entry point with Firebase initialization
├── task_app.dart # Main app widget and home page with auth wrapper
├── task_list.dart # Widget to display tasks from Firestore
├── auth_service.dart # Authentication service with error handling
└── auth_screens.dart # Login and signup screens
The app uses fake_cloud_firestore for Firestore testing and firebase_auth_mocks for authentication testing, which simulate Firebase behavior without requiring a real Firebase connection.
Key Testing Concepts:
- Fake Database:
FakeFirebaseFirestore()simulates Firestore - Mock Authentication:
MockAuthServiceimplementsAuthServiceInterfacefor testing - Data Population: Pre-populate fake database with test data
- Stream Testing: Test real-time updates using
tester.idle()andtester.pump() - Widget Interaction: Test user interactions like tapping buttons and checkboxes
- Form Validation: Test input validation and error handling
Firestore Tests:
- ✅ Display tasks from Firestore
- ✅ Show empty state when no tasks
- ✅ Toggle task completion
- ✅ Loading states
- ✅ Individual widget testing
Authentication Tests:
- ✅ Login form validation (email, password, format)
- ✅ Signup form validation (email, password, confirmation)
- ✅ Loading states during authentication
- ✅ Error handling and user feedback
- ✅ Form field validation
# Run all tests
flutter test
# Run specific test file
flutter test test/task_app_test.dartThis demo uses FlutterFire CLI for Firebase configuration:
# Install FlutterFire CLI
dart pub global activate flutterfire_cli
# Configure Firebase for your project
flutterfire configurefirebase_core: Firebase initializationcloud_firestore: Firestore databasefirebase_auth: Firebase Authenticationfake_cloud_firestore: Testing fakes (dev dependency)firebase_auth_mocks: Authentication testing (dev dependency)
- Use Fakes for Unit Tests: Avoid real Firebase calls in tests
- Interface-based Design: Use abstract classes for better testability
- Mock Authentication: Test auth flows without Firebase initialization
- Test Stream Behavior: Handle async streams properly with
tester.idle() - Isolate Components: Test individual widgets separately
- Mock User Interactions: Test button taps, form submissions, etc.
- Verify UI States: Check loading, empty, and populated states
- Form Validation Testing: Test input validation and error handling
For integration tests with the Firestore emulator:
- Start Firestore emulator:
firebase emulators:start --only firestore - Configure app to use emulator (see
test/integration_test.dartexample) - Run integration tests