Skip to content

Instantly share code, notes, and snippets.

@SatyaAchanta
Created January 2, 2022 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SatyaAchanta/f81790e022621629030069ca55d66973 to your computer and use it in GitHub Desktop.
Save SatyaAchanta/f81790e022621629030069ca55d66973 to your computer and use it in GitHub Desktop.
Unit Test Dart + Firestore collection calls
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);
});
});
}
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