Skip to content
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

Write Unit Tests for Business Logic #26

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,6 @@ app.*.map.json
!.vscode/extensions.json
!.vscode/launch.json
!.idea/codeStyles/
!.idea/dictionaries/
!.idea/dictionaries/
# No need to push mock files since they will be generated automatically while building
*.mocks.dart
7 changes: 7 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1"
mockito:
dependency: "direct main"
description:
name: mockito
url: "https://pub.dartlang.org"
source: hosted
version: "5.1.0"
mocktail:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ dependencies:
path: ./praxis_flutter_domain
praxis_data:
path: ./praxis_data
mockito: ^5.1.0


dev_dependencies:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:praxis_data/sources/network/exceptions/api_exception.dart';
import 'package:praxis_flutter_domain/entities/api_response.dart'
as api_response;
import 'package:praxis_flutter_domain/entities/jokes/dm_joke_list.dart';
import 'package:praxis_flutter_domain/repositories/jokes/jokes_repository.dart';
import 'package:praxis_flutter_domain/use_cases/get_five_random_jokes_usecase.dart';

import 'get_five_random_jokes_usecase_should.mocks.dart';

@GenerateMocks([JokesRepository])
void main() {
late MockJokesRepository _mockJokesRepo;
late GetFiveRandomJokesUseCase _sut;

setUp(
() {
_mockJokesRepo = MockJokesRepository();
_sut = GetFiveRandomJokesUseCase(_mockJokesRepo);
},
);

test(
"return five random jokes on success API response",
() async {
var testApiResponse =
api_response.Success(data: JokesListWithType("testType", []));

when(_mockJokesRepo.getFiveRandomJokes())
.thenAnswer((realInvocation) async => testApiResponse);

_sut.perform((data) {
if (data != null) {
expect(data.jokeList, testApiResponse);
}
}, (Object error) {}, () {});
},
);

test(
"return error on failure API response",
() async {
var testApiResponse = api_response.Failure<JokesListWithType>(
error: APIException("testException", 404, "testStatus"));

when(_mockJokesRepo.getFiveRandomJokes())
.thenAnswer((realInvocation) async => testApiResponse);

_sut.perform((data) {
if (data != null) {
expect(
((data.jokeList as api_response.Failure<JokesListWithType>).error
as APIException)
.statusCode,
404);
}
}, (Object error) {}, () {});
},
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:praxis_flutter_domain/entities/login_request.dart';
import 'package:praxis_flutter_domain/entities/login_result.dart';
import 'package:praxis_flutter_domain/repositories/login_repo.dart';
import 'package:praxis_flutter_domain/use_cases/login_use_case.dart';

import 'login_use_case_should.mocks.dart';

@GenerateMocks([LoginRepo])
void main() {
late MockLoginRepo _mockLoginRepo;
late LoginUseCase _sut;

setUp(
() {
_mockLoginRepo = MockLoginRepo();
_sut = LoginUseCase(_mockLoginRepo);
},
);

test(
"return login result on valid email and password",
() {
when(_mockLoginRepo.login("testUsername", "testPassword"))
.thenAnswer((_) async => const LoginResult("testJwt", "testToken"));

_sut.perform((event) {
if (event != null) {
expect(event, const LoginResult("testJwt", "testToken"));
}
}, (Object error) {}, () {},
LoginRequest("testUsername", "testPassword"));
},
);
}