diff --git a/test/service/todo_service_test.dart b/test/service/todo_service_test.dart index 32459bd..edee3ba 100644 --- a/test/service/todo_service_test.dart +++ b/test/service/todo_service_test.dart @@ -36,21 +36,40 @@ void main() { todoService = container.read(todoServiceProvider); }); + void initializeWhenCategory(String categoryId) { + final categoryCollectionRef = MockCollectionReference(); + final categoryDocumentRef = MockDocumentReference(); + + when( + () => mockFirestoreInstance.collection(categoryCollection), + ).thenReturn(categoryCollectionRef); + when( + () => categoryCollectionRef.doc(categoryId), + ).thenAnswer((_) => categoryDocumentRef); + when(() => categoryDocumentRef.update(any())).thenAnswer( + (_) => Future.value(), + ); + } + group('$TodoService', () { test('Get $Todo list from Firebase mock', () async { // arrange - when(() => mockFirestoreInstance.collection(any())) - .thenReturn(mockCollectionReference); + const fieldName = 'categoryId'; + + when( + () => mockFirestoreInstance.collection(todoCollection), + ).thenReturn(mockCollectionReference); when( () => mockCollectionReference.where( - any(), + fieldName, isEqualTo: any(named: 'isEqualTo'), ), ).thenReturn(mockQuery); - when(() => mockQuery.snapshots()) - .thenAnswer((_) => Stream.value(mockQuerySnapshot)); + when( + () => mockQuery.snapshots(), + ).thenAnswer((_) => Stream.value(mockQuerySnapshot)); when(() => mockQuerySnapshot.docs) .thenReturn([mockQueryDocumentSnapshot]); @@ -66,26 +85,29 @@ void main() { expect(result, isA>>()); expect(finalResult, isA>()); expect(finalResult, [existingTodo]); + verify( () => mockFirestoreInstance - .collection(any()) - .where(any(), isEqualTo: any(named: 'isEqualTo')), + .collection(todoCollection) + .where(fieldName, isEqualTo: any(named: 'isEqualTo')), ).called(1); }); test('Get $Todo by id from Firebase mock', () async { // arrange - when(() => mockFirestoreInstance.collection(any())) - .thenReturn(mockCollectionReference); + when( + () => mockFirestoreInstance.collection(todoCollection), + ).thenReturn(mockCollectionReference); - when(() => mockCollectionReference.doc(any())) - .thenReturn(mockDocumentReference); + when( + () => mockCollectionReference.doc(todoId), + ).thenReturn(mockDocumentReference); - when(() => mockDocumentReference.get()) - .thenAnswer((_) => Future.value(mockDocumentSnapshot)); + when( + () => mockDocumentReference.get(), + ).thenAnswer((_) => Future.value(mockDocumentSnapshot)); when(() => mockDocumentSnapshot.exists).thenReturn(true); - when(() => mockDocumentSnapshot.toMap).thenReturn(existingTodo.toJson()); // act @@ -96,23 +118,27 @@ void main() { expect(result, isA>()); expect(finalResult, isA()); expect(finalResult, existingTodo); - verify(() => mockFirestoreInstance.collection(any()).doc(any())) - .called(1); + + verify( + () => mockFirestoreInstance.collection(todoCollection).doc(todoId), + ).called(1); }); test("Get $Exception if todo by id doesn't exists", () async { // arrange - when(() => mockFirestoreInstance.collection(any())) - .thenReturn(mockCollectionReference); + when( + () => mockFirestoreInstance.collection(todoCollection), + ).thenReturn(mockCollectionReference); - when(() => mockCollectionReference.doc(any())) - .thenReturn(mockDocumentReference); + when( + () => mockCollectionReference.doc(todoId), + ).thenReturn(mockDocumentReference); - when(() => mockDocumentReference.get()) - .thenAnswer((_) => Future.value(mockDocumentSnapshot)); + when( + () => mockDocumentReference.get(), + ).thenAnswer((_) => Future.value(mockDocumentSnapshot)); when(() => mockDocumentSnapshot.exists).thenReturn(false); - when(() => mockDocumentSnapshot.toMap).thenThrow( Exception('Oops!!! Todo not found'), ); @@ -121,76 +147,95 @@ void main() { final result = todoService.getTodoById(categoryId, existingTodo.id!); // assert - expect(result, throwsA(isA())); - verify(() => mockFirestoreInstance.collection(any()).doc(any())) - .called(1); + await expectLater(result, throwsA(isA())); + + verify( + () => mockFirestoreInstance.collection(todoCollection).doc(todoId), + ).called(1); }); test('Save $Todo from Firebase mock', () { // arrange - when(() => mockFirestoreInstance.collection(any())) - .thenReturn(mockCollectionReference); - - when(() => mockCollectionReference.add(any())) - .thenAnswer((_) async => mockDocumentReference); + initializeWhenCategory(initialTodo.categoryId); - when(() => mockCollectionReference.doc(any())) - .thenReturn(mockDocumentReference); - - when(() => mockDocumentReference.update(any())) - .thenAnswer((_) => Future.value()); + when( + () => mockFirestoreInstance.collection(todoCollection), + ).thenReturn(mockCollectionReference); + when( + () => mockCollectionReference.add(initialTodo.toJson()), + ).thenAnswer((_) async => mockDocumentReference); // act final result = todoService.saveTodo(initialTodo); // assert expect(result, isA>()); - verify(() => mockFirestoreInstance.collection(any()).add(any())) - .called(1); + + verify( + () => mockFirestoreInstance + .collection(todoCollection) + .add(initialTodo.toJson()), + ).called(1); }); test('Update $Todo from Firebase mock', () { // arrange - when(() => mockFirestoreInstance.collection(any())) - .thenReturn(mockCollectionReference); + final path = existingTodo.id!; + final data = existingTodo.toJson(); - when(() => mockCollectionReference.doc(any())) - .thenReturn(mockDocumentReference); + when( + () => mockFirestoreInstance.collection(todoCollection), + ).thenReturn(mockCollectionReference); - when(() => mockDocumentReference.update(any())) - .thenAnswer((_) => Future.value()); + when( + () => mockCollectionReference.doc(path), + ).thenReturn(mockDocumentReference); + + when( + () => mockDocumentReference.update(data), + ).thenAnswer((_) => Future.value()); // act final result = todoService.saveTodo(existingTodo.copyWith()); // assert expect(result, isA>()); + verify( - () => mockFirestoreInstance.collection(any()).doc(any()).update(any()), - ); + () => mockFirestoreInstance + .collection(todoCollection) + .doc(path) + .update(data), + ).called(1); }); test('Remove $Todo from Firebase mock', () { // arrange - when(() => mockFirestoreInstance.collection(any())) - .thenReturn(mockCollectionReference); - - when(() => mockCollectionReference.doc(any())) - .thenReturn(mockDocumentReference); + initializeWhenCategory(existingTodo.categoryId); - when(() => mockDocumentReference.delete()) - .thenAnswer((_) => Future.value()); - - when(() => mockDocumentReference.update(any())) - .thenAnswer((_) => Future.value()); + when( + () => mockFirestoreInstance.collection(todoCollection), + ).thenReturn(mockCollectionReference); + when( + () => mockCollectionReference.doc(existingTodo.id), + ).thenReturn(mockDocumentReference); + when(mockDocumentReference.delete).thenAnswer((_) => Future.value()); // act - final result = todoService.deleteTodo(existingTodo.id!, category.id!); + final result = todoService.deleteTodo( + existingTodo.id!, + existingTodo.categoryId, + ); // assert expect(result, isA>()); - verify(() => mockFirestoreInstance.collection(any()).doc(any()).delete()) - .called(1); + + verify( + () => mockFirestoreInstance + .collection(todoCollection) + .doc(existingTodo.id) + .delete(), + ).called(1); }); }); }