Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,5 @@ app.*.symbols
*.freezed.dart
*.g.dart
*/di.config.dart
*.mocks.dart
*.mocks.dart
lib/core/di/di.config.dart
10 changes: 10 additions & 0 deletions test/layers/domain/fakes/fake_book_domain.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:mibook/layers/domain/models/book_list_domain.dart';

final fakeBookDomain = BookDomain(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For one moment I was wondering what the hell is a FacebookDomain....

id: 'id',
title: 'title',
description: 'description',
thumbnail: 'thumbnail',
authors: [],
kind: 'kind',
);
9 changes: 9 additions & 0 deletions test/layers/domain/fakes/fake_book_list_domain.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

import 'package:mibook/layers/domain/models/book_list_domain.dart';

import 'fake_book_domain.dart';

final fakeBookListDomain = BookListDomain(
totalItems: 1,
books: [fakeBookDomain],
);
52 changes: 52 additions & 0 deletions test/layers/domain/get_book_details_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:mibook/layers/domain/repository/search_repository.dart';
import 'package:mibook/layers/domain/usecases/get_book_details.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'fakes/fake_book_domain.dart';

@GenerateNiceMocks([MockSpec<ISearchRepository>()])
import 'get_book_details_test.mocks.dart';

void main() {
late MockISearchRepository mockSearchRepository;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we avoid late variables in the tests? That could provoke crashes in our CI.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t understand why this would cause any issues in our CI/CD pipeline, since we’re initializing them properly in the setUp method.

Anyways, I will merge the PR. If you think it is important to address this in the future, let's create an issue in our repo.

🤗

late GetBookDetails getBookDetails;

setUp(() {
mockSearchRepository = MockISearchRepository();
getBookDetails = GetBookDetails(
mockSearchRepository,
);
});

group('GetBookDetails', () {
test('call returns BookDetails on success', () async {
when(
mockSearchRepository.searchById(
id: 'id',
),
).thenAnswer((_) async => Future.value(fakeBookDomain));

final result = await getBookDetails.call(id: 'id');
expect(result, fakeBookDomain);
});

test('call returns BookDetails on failure', () async {
when(
mockSearchRepository.searchById(
id: 'id',
),
).thenThrow(Exception('Something went wrong'));

var error = false;
try {
await getBookDetails.call(id: 'id');
error = false;
} catch (e) {
error = true;
}

expect(error, isTrue);
});
});
}
61 changes: 61 additions & 0 deletions test/layers/domain/search_books_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:mibook/layers/domain/repository/search_repository.dart';
import 'package:mibook/layers/domain/usecases/search_books.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';

import 'fakes/fake_book_list_domain.dart';

@GenerateNiceMocks([MockSpec<ISearchRepository>()])
import 'search_books_test.mocks.dart';

void main() {
late MockISearchRepository mockSearchRepository;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

late SearchBooks searchBooks;

setUp(() {
mockSearchRepository = MockISearchRepository();
searchBooks = SearchBooks(
mockSearchRepository,
);
});

group('SearchBooks', () {
test('call returns BookListDomain on success', () async {
when(
mockSearchRepository.searchByTitle(
initTitle: 'initTitle',
startIndex: 0,
),
).thenAnswer((_) async => Future.value(fakeBookListDomain));

final result = await searchBooks.call(
initTitle: 'initTitle',
startIndex: 0,
);
expect(result, fakeBookListDomain);
});

test('call returns BookListDomain on failure', () async {
when(
mockSearchRepository.searchByTitle(
initTitle: 'initTitle',
startIndex: 0,
),
).thenThrow(Exception('Something went wrong'));

var error = false;
try {
await searchBooks.call(
initTitle: 'initTitle',
startIndex: 0,
);
error = false;
} catch (e) {
error = true;
}

expect(error, isTrue);
});
});
}