forked from eatvio/eatvio_app
test(tests): add comprehensive unit tests for various models and repositories, enhancing test coverage and ensuring correct functionality
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:eatvio_app/features/create_plan/repositories/create_plan_repository.dart';
|
||||
import 'package:eatvio_app/features/create_plan/services/create_plan_service.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
|
||||
import 'create_plan_repository_test.mocks.dart';
|
||||
|
||||
@GenerateNiceMocks([MockSpec<CreatePlanService>()])
|
||||
void main() {
|
||||
group('CreatePlanRepository', () {
|
||||
late CreatePlanRepository repository;
|
||||
late MockCreatePlanService mockService;
|
||||
|
||||
setUp(() {
|
||||
mockService = MockCreatePlanService();
|
||||
repository = CreatePlanRepository(createPlanService: mockService);
|
||||
});
|
||||
|
||||
test('createPlan - успешное создание плана', () async {
|
||||
final model = {
|
||||
'name': 'Test Plan',
|
||||
'description': 'Test Description',
|
||||
'duration': 7,
|
||||
'type': 'fit',
|
||||
};
|
||||
|
||||
when(mockService.createPlan(formData: anyNamed('formData')))
|
||||
.thenAnswer((_) async => Future.value());
|
||||
|
||||
await repository.createPlan(model: model);
|
||||
|
||||
verify(mockService.createPlan(formData: anyNamed('formData'))).called(1);
|
||||
});
|
||||
|
||||
test('createPlan - преобразует модель в FormData', () async {
|
||||
final model = {
|
||||
'name': 'Test Plan',
|
||||
'description': 'Test Description',
|
||||
'duration': 7,
|
||||
'type': 'fit',
|
||||
'image': 'test_image.jpg',
|
||||
};
|
||||
|
||||
when(mockService.createPlan(formData: anyNamed('formData')))
|
||||
.thenAnswer((_) async => Future.value());
|
||||
|
||||
await repository.createPlan(model: model);
|
||||
|
||||
final captured = verify(mockService.createPlan(formData: captureAnyNamed('formData')))
|
||||
.captured;
|
||||
expect(captured.length, 1);
|
||||
expect(captured.first, isA<FormData>());
|
||||
});
|
||||
|
||||
test('createPlan - пробрасывает исключение при ошибке', () async {
|
||||
final model = {'name': 'Test Plan'};
|
||||
final exception = Exception('Ошибка создания плана');
|
||||
|
||||
when(mockService.createPlan(formData: anyNamed('formData')))
|
||||
.thenThrow(exception);
|
||||
|
||||
expect(
|
||||
() => repository.createPlan(model: model),
|
||||
throwsException,
|
||||
);
|
||||
});
|
||||
|
||||
test('createPlan - обрабатывает пустую модель', () async {
|
||||
final model = <String, dynamic>{};
|
||||
|
||||
when(mockService.createPlan(formData: anyNamed('formData')))
|
||||
.thenAnswer((_) async => Future.value());
|
||||
|
||||
await repository.createPlan(model: model);
|
||||
|
||||
verify(mockService.createPlan(formData: anyNamed('formData'))).called(1);
|
||||
});
|
||||
|
||||
test('createPlan - обрабатывает модель с вложенными данными', () async {
|
||||
final model = {
|
||||
'name': 'Test Plan',
|
||||
'meals': [
|
||||
{'id': 1, 'name': 'Breakfast'},
|
||||
{'id': 2, 'name': 'Lunch'},
|
||||
],
|
||||
};
|
||||
|
||||
when(mockService.createPlan(formData: anyNamed('formData')))
|
||||
.thenAnswer((_) async => Future.value());
|
||||
|
||||
await repository.createPlan(model: model);
|
||||
|
||||
verify(mockService.createPlan(formData: anyNamed('formData'))).called(1);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user