-
Notifications
You must be signed in to change notification settings - Fork 0
Some Unit tests #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Some Unit tests #11
Changes from all commits
f149134
a4d8b27
bb8746d
e35d959
6e90918
67cce3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -168,4 +168,5 @@ app.*.symbols | |
| *.freezed.dart | ||
| *.g.dart | ||
| */di.config.dart | ||
| *.mocks.dart | ||
| *.mocks.dart | ||
| lib/core/di/di.config.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( | ||
| id: 'id', | ||
| title: 'title', | ||
| description: 'description', | ||
| thumbnail: 'thumbnail', | ||
| authors: [], | ||
| kind: 'kind', | ||
| ); | ||
| 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], | ||
| ); |
| 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; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we avoid
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| }); | ||
| }); | ||
| } | ||
| 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; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| }); | ||
| }); | ||
| } | ||
There was a problem hiding this comment.
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....