Skip to content

Commit

Permalink
Modify unit test methods
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianVarela committed Nov 26, 2024
1 parent 8f0339a commit a346686
Showing 1 changed file with 104 additions and 59 deletions.
163 changes: 104 additions & 59 deletions test/service/todo_service_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -66,26 +85,29 @@ void main() {
expect(result, isA<Stream<List<Todo>>>());
expect(finalResult, isA<List<Todo>>());
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
Expand All @@ -96,23 +118,27 @@ void main() {
expect(result, isA<Future<Todo>>());
expect(finalResult, isA<Todo>());
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'),
);
Expand All @@ -121,76 +147,95 @@ void main() {
final result = todoService.getTodoById(categoryId, existingTodo.id!);

// assert
expect(result, throwsA(isA<Exception>()));
verify(() => mockFirestoreInstance.collection(any()).doc(any()))
.called(1);
await expectLater(result, throwsA(isA<Exception>()));

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<Future<void>>());
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<Future<void>>());

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<Future<void>>());
verify(() => mockFirestoreInstance.collection(any()).doc(any()).delete())
.called(1);

verify(
() => mockFirestoreInstance
.collection(todoCollection)
.doc(existingTodo.id)
.delete(),
).called(1);
});
});
}

0 comments on commit a346686

Please sign in to comment.