test(tests): add comprehensive unit tests for various models and repositories, enhancing test coverage and ensuring correct functionality

This commit is contained in:
mso7nueh
2025-12-09 16:19:48 +03:00
parent edd064d20d
commit 70e4a42e98
25 changed files with 2565 additions and 58 deletions

View File

@@ -0,0 +1,204 @@
import 'package:dio/dio.dart';
import 'package:eatvio_app/features/registration/models/location_model.dart';
import 'package:eatvio_app/features/registration/models/user_registration_model.dart';
import 'package:eatvio_app/features/registration/repositories/registration_repository.dart';
import 'package:eatvio_app/features/registration/services/registration_service.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:shared_preferences/shared_preferences.dart';
class MockRegistrationService extends Mock implements RegistrationService {}
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
group('RegistrationRepository', () {
late RegistrationRepository repository;
late MockRegistrationService mockService;
setUp(() {
mockService = MockRegistrationService();
repository = RegistrationRepository(registrationService: mockService);
SharedPreferences.setMockInitialValues({});
});
test('tryRegister - возвращает токен при успешной регистрации', () async {
const token = 'test-token-123';
const sheet = UserRegistrationSheet(
firstName: 'Test',
lastName: 'User',
email: 'test@example.com',
password: 'password123',
passwordConfirmation: 'password123',
birthdate: '1990-01-01',
gender: 'male',
location: 'Moscow',
responseBody: '{}',
lang: 'ru',
weight: 70,
height: 175,
phone: '+1234567890',
chestCircumference: 100,
waistCircumference: 80,
hipCircumference: 90,
bloodPressureUp: 120,
bloodPressureDown: 80,
hasHypo: 0,
hasHyper: 0,
aboutAllergy: '',
aboutTakingPils: '',
aboutSportHobbies: '',
favoriteFoods: '',
unfavoriteFoods: '',
additionalInfo: '',
);
when(() => mockService.register(sheet: any(named: 'sheet')))
.thenAnswer((_) async => {'token': token});
final result = await repository.tryRegister(sheet);
expect(result, token);
verify(() => mockService.register(sheet: any(named: 'sheet'))).called(1);
});
test('tryRegister - выбрасывает исключение при отсутствии токена', () async {
const sheet = UserRegistrationSheet(
firstName: 'Test',
lastName: 'User',
email: 'test@example.com',
password: 'password123',
passwordConfirmation: 'password123',
birthdate: '1990-01-01',
gender: 'male',
location: 'Moscow',
responseBody: '{}',
lang: 'ru',
weight: 70,
height: 175,
phone: '+1234567890',
chestCircumference: 100,
waistCircumference: 80,
hipCircumference: 90,
bloodPressureUp: 120,
bloodPressureDown: 80,
hasHypo: 0,
hasHyper: 0,
aboutAllergy: '',
aboutTakingPils: '',
aboutSportHobbies: '',
favoriteFoods: '',
unfavoriteFoods: '',
additionalInfo: '',
);
when(() => mockService.register(sheet: any(named: 'sheet')))
.thenAnswer((_) async => {'message': 'Error message'});
expect(() => repository.tryRegister(sheet), throwsA(isA<String>()));
});
test('tryRegister - обрабатывает DioException с кодом 422', () async {
const sheet = UserRegistrationSheet(
firstName: 'Test',
lastName: 'User',
email: 'test@example.com',
password: 'password123',
passwordConfirmation: 'password123',
birthdate: '1990-01-01',
gender: 'male',
location: 'Moscow',
responseBody: '{}',
lang: 'ru',
weight: 70,
height: 175,
phone: '+1234567890',
chestCircumference: 100,
waistCircumference: 80,
hipCircumference: 90,
bloodPressureUp: 120,
bloodPressureDown: 80,
hasHypo: 0,
hasHyper: 0,
aboutAllergy: '',
aboutTakingPils: '',
aboutSportHobbies: '',
favoriteFoods: '',
unfavoriteFoods: '',
additionalInfo: '',
);
final dioException = DioException(
requestOptions: RequestOptions(path: '/api/register'),
response: Response(
requestOptions: RequestOptions(path: '/api/register'),
statusCode: 422,
data: {
'message': 'Validation error',
'errors': {
'email': ['Email already exists'],
},
},
),
);
when(() => mockService.register(sheet: any(named: 'sheet')))
.thenThrow(dioException);
expect(() => repository.tryRegister(sheet), throwsA(isA<String>()));
});
test('getLocationsListByQuery - возвращает LocationModel', () async {
const query = 'Moscow';
const expected = LocationModel(
locations: ['Moscow, Russia', 'Moscow Oblast, Russia'],
responseBody: '{}',
);
when(() => mockService.getLocationsByAddress(query: query))
.thenAnswer((_) async => expected);
final result = await repository.getLocationsListByQuery(query);
expect(result, expected);
verify(() => mockService.getLocationsByAddress(query: query)).called(1);
});
test('getLocationsListByQuery - пробрасывает исключение при ошибке', () async {
const query = 'Moscow';
when(() => mockService.getLocationsByAddress(query: query))
.thenThrow(Exception('Error'));
expect(() => repository.getLocationsListByQuery(query), throwsException);
});
test('getLocationsListByCoordinates - возвращает LocationModel', () async {
const latitude = '55.7558';
const longitude = '37.6173';
const expected = LocationModel(
locations: ['Moscow, Russia'],
responseBody: '{}',
);
when(() => mockService.getLocationsByCoordinate(latitude: latitude, longitude: longitude))
.thenAnswer((_) async => expected);
final result = await repository.getLocationsListByCoordinates(latitude, longitude);
expect(result, expected);
verify(() => mockService.getLocationsByCoordinate(latitude: latitude, longitude: longitude)).called(1);
});
test('getLocationsListByCoordinates - пробрасывает исключение при ошибке', () async {
const latitude = '55.7558';
const longitude = '37.6173';
when(() => mockService.getLocationsByCoordinate(latitude: latitude, longitude: longitude))
.thenThrow(Exception('Error'));
expect(() => repository.getLocationsListByCoordinates(latitude, longitude), throwsException);
});
});
}