Created
January 2, 2022 00:17
-
-
Save SatyaAchanta/f81790e022621629030069ca55d66973 to your computer and use it in GitHub Desktop.
Unit Test Dart + Firestore collection calls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:fake_cloud_firestore/fake_cloud_firestore.dart'; | |
import 'package:firebase_core/firebase_core.dart'; | |
import 'package:flutter_test/flutter_test.dart'; | |
import 'package:hairstylist_appointment/models/appointment.dart'; | |
import 'package:hairstylist_appointment/services/appointments_service.dart'; | |
import '../common/collection_reference_mock.dart'; | |
import '../firebase_mock.dart'; | |
void main() { | |
setupFirebaseAuthMocks(); | |
final firestoreCollection = | |
FakeFirebaseFirestore().collection("appointments"); | |
setUpAll(() async { | |
await Firebase.initializeApp(); | |
}); | |
group("Test Appointment Service", () { | |
test("schedule appointment should return true on successful scheduling", | |
() async { | |
final appointmentService = AppointmentService(); | |
appointmentService.appointmentCollection = firestoreCollection; | |
Appointment mockAppointment = | |
new Appointment(appointmentDate: DateTime.now()); | |
bool scheduleAppointmentResponse = await appointmentService | |
.scheduleAppointment(mockAppointment, "useremail"); | |
expect(scheduleAppointmentResponse, true); | |
}); | |
test( | |
"schedule appointment should return false on failing to schedule appointment", | |
() async { | |
final appointmentService = AppointmentService(); | |
appointmentService.appointmentCollection = MockCollectionReference(); | |
Appointment mockAppointment = | |
new Appointment(appointmentDate: DateTime.now()); | |
bool scheduleAppointmentResponse = await appointmentService | |
.scheduleAppointment(mockAppointment, "useremail"); | |
expect(scheduleAppointmentResponse, false); | |
}); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:cloud_firestore/cloud_firestore.dart'; | |
import 'package:mockito/mockito.dart'; | |
class MockCollectionReference extends Mock implements CollectionReference { | |
@override | |
DocumentReference<Object?> doc([String? path]) { | |
throw FirebaseException(plugin: "firestore"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment