Skip to content

Commit 79371b6

Browse files
authored
Merge pull request #11 from CodandoTV/feature/unit-tests-impro
Some Unit tests
2 parents 3e4d604 + 67cce3b commit 79371b6

6 files changed

Lines changed: 134 additions & 1 deletion

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,5 @@ app.*.symbols
168168
*.freezed.dart
169169
*.g.dart
170170
*/di.config.dart
171-
*.mocks.dart
171+
*.mocks.dart
172+
lib/core/di/di.config.dart

test/search_data_source_test.dart renamed to test/layers/data/datasource/search_data_source_test.dart

File renamed without changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import 'package:mibook/layers/domain/models/book_list_domain.dart';
2+
3+
final fakeBookDomain = BookDomain(
4+
id: 'id',
5+
title: 'title',
6+
description: 'description',
7+
thumbnail: 'thumbnail',
8+
authors: [],
9+
kind: 'kind',
10+
);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
import 'package:mibook/layers/domain/models/book_list_domain.dart';
3+
4+
import 'fake_book_domain.dart';
5+
6+
final fakeBookListDomain = BookListDomain(
7+
totalItems: 1,
8+
books: [fakeBookDomain],
9+
);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:mibook/layers/domain/repository/search_repository.dart';
3+
import 'package:mibook/layers/domain/usecases/get_book_details.dart';
4+
import 'package:mockito/annotations.dart';
5+
import 'package:mockito/mockito.dart';
6+
import 'fakes/fake_book_domain.dart';
7+
8+
@GenerateNiceMocks([MockSpec<ISearchRepository>()])
9+
import 'get_book_details_test.mocks.dart';
10+
11+
void main() {
12+
late MockISearchRepository mockSearchRepository;
13+
late GetBookDetails getBookDetails;
14+
15+
setUp(() {
16+
mockSearchRepository = MockISearchRepository();
17+
getBookDetails = GetBookDetails(
18+
mockSearchRepository,
19+
);
20+
});
21+
22+
group('GetBookDetails', () {
23+
test('call returns BookDetails on success', () async {
24+
when(
25+
mockSearchRepository.searchById(
26+
id: 'id',
27+
),
28+
).thenAnswer((_) async => Future.value(fakeBookDomain));
29+
30+
final result = await getBookDetails.call(id: 'id');
31+
expect(result, fakeBookDomain);
32+
});
33+
34+
test('call returns BookDetails on failure', () async {
35+
when(
36+
mockSearchRepository.searchById(
37+
id: 'id',
38+
),
39+
).thenThrow(Exception('Something went wrong'));
40+
41+
var error = false;
42+
try {
43+
await getBookDetails.call(id: 'id');
44+
error = false;
45+
} catch (e) {
46+
error = true;
47+
}
48+
49+
expect(error, isTrue);
50+
});
51+
});
52+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:mibook/layers/domain/repository/search_repository.dart';
3+
import 'package:mibook/layers/domain/usecases/search_books.dart';
4+
import 'package:mockito/annotations.dart';
5+
import 'package:mockito/mockito.dart';
6+
7+
import 'fakes/fake_book_list_domain.dart';
8+
9+
@GenerateNiceMocks([MockSpec<ISearchRepository>()])
10+
import 'search_books_test.mocks.dart';
11+
12+
void main() {
13+
late MockISearchRepository mockSearchRepository;
14+
late SearchBooks searchBooks;
15+
16+
setUp(() {
17+
mockSearchRepository = MockISearchRepository();
18+
searchBooks = SearchBooks(
19+
mockSearchRepository,
20+
);
21+
});
22+
23+
group('SearchBooks', () {
24+
test('call returns BookListDomain on success', () async {
25+
when(
26+
mockSearchRepository.searchByTitle(
27+
initTitle: 'initTitle',
28+
startIndex: 0,
29+
),
30+
).thenAnswer((_) async => Future.value(fakeBookListDomain));
31+
32+
final result = await searchBooks.call(
33+
initTitle: 'initTitle',
34+
startIndex: 0,
35+
);
36+
expect(result, fakeBookListDomain);
37+
});
38+
39+
test('call returns BookListDomain on failure', () async {
40+
when(
41+
mockSearchRepository.searchByTitle(
42+
initTitle: 'initTitle',
43+
startIndex: 0,
44+
),
45+
).thenThrow(Exception('Something went wrong'));
46+
47+
var error = false;
48+
try {
49+
await searchBooks.call(
50+
initTitle: 'initTitle',
51+
startIndex: 0,
52+
);
53+
error = false;
54+
} catch (e) {
55+
error = true;
56+
}
57+
58+
expect(error, isTrue);
59+
});
60+
});
61+
}

0 commit comments

Comments
 (0)