feat(tests): add fixtures

This commit is contained in:
mso7nueh
2025-11-20 16:58:56 +03:00
parent 6fa944b3df
commit 4deda92c5e
24 changed files with 6852 additions and 4358 deletions

View File

@@ -1,27 +1,27 @@
import 'package:eatvio_app/features/auth/models/token_model.dart';
import 'package:eatvio_app/features/auth/presentation/bloc/auth_cubit/auth_cubit.dart';
import 'package:eatvio_app/features/auth/presentation/bloc/auth_cubit/auth_state.dart';
import 'package:eatvio_app/features/auth/repositories/auth_repository.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'auth_cubit_test.mocks.dart';
import '../../../../fixtures/doubles/auth_doubles.dart';
import '../../../../fixtures/fixture_reader.dart';
@GenerateNiceMocks([MockSpec<AuthRepository>()])
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
group('AuthCubit', () {
AuthCubit? authCubit;
late MockAuthRepository mockAuthRepository;
late FixtureAuthRepository authRepository;
late Map<String, dynamic> credentialsFixture;
const testToken = TokenModel(token: 'test_token_12345');
late TokenModel tokenFixture;
setUp(() async {
mockAuthRepository = MockAuthRepository();
tokenFixture = TokenModel.fromJson(FixtureReader.readAsMap('auth/token_response.json'));
credentialsFixture = FixtureReader.readAsMap('auth/login_credentials.json');
authRepository = FixtureAuthRepository(tokenFixture: tokenFixture);
SharedPreferences.setMockInitialValues({});
// Mock Fluttertoast plugin
@@ -50,31 +50,37 @@ void main() {
});
test('начальное состояние - AuthState', () {
authCubit = AuthCubit(authRepository: mockAuthRepository);
authCubit = AuthCubit(authRepository: authRepository);
expect(authCubit!.state, const AuthState());
});
test('getTokenByCredentials - успешная авторизация', () async {
authCubit = AuthCubit(authRepository: mockAuthRepository);
when(mockAuthRepository.getTokenByCredentials(any, any))
.thenAnswer((_) async => testToken);
authCubit = AuthCubit(authRepository: authRepository);
final result = await authCubit!.getTokenByCredentials('test@example.com', 'password123');
final result = await authCubit!.getTokenByCredentials(
credentialsFixture['email'] as String,
credentialsFixture['password'] as String,
);
expect(result, true);
await Future.delayed(const Duration(milliseconds: 100));
expect(authCubit!.state, isA<AuthStateLoaded>());
final loadedState = authCubit!.state as AuthStateLoaded;
expect(loadedState.token, 'test_token_12345');
verify(mockAuthRepository.getTokenByCredentials('test@example.com', 'password123')).called(1);
expect(loadedState.token, tokenFixture.token);
expect(
authRepository.credentialPayloads.single,
credentialsFixture.map((key, value) => MapEntry(key, value.toString())),
);
});
test('getTokenByCredentials - переход в состояние loading', () async {
authCubit = AuthCubit(authRepository: mockAuthRepository);
when(mockAuthRepository.getTokenByCredentials(any, any))
.thenAnswer((_) async => Future.delayed(const Duration(milliseconds: 50)).then((_) => testToken));
authCubit = AuthCubit(authRepository: authRepository);
authRepository.responseDelay = const Duration(milliseconds: 50);
final future = authCubit!.getTokenByCredentials('test@example.com', 'password123');
final future = authCubit!.getTokenByCredentials(
credentialsFixture['email'] as String,
credentialsFixture['password'] as String,
);
await Future.delayed(const Duration(milliseconds: 10));
expect(authCubit!.state, isA<AuthStateLoading>());
@@ -82,9 +88,8 @@ void main() {
});
test('getTokenByCredentials - обработка ошибки авторизации', () async {
authCubit = AuthCubit(authRepository: mockAuthRepository);
when(mockAuthRepository.getTokenByCredentials(any, any))
.thenThrow(Exception('Неверный адрес электронной почты или пароль'));
authCubit = AuthCubit(authRepository: authRepository);
authRepository.error = Exception('Неверный адрес электронной почты или пароль');
final result = await authCubit!.getTokenByCredentials('wrong@example.com', 'wrongpassword');
@@ -93,13 +98,15 @@ void main() {
expect(authCubit!.state, isA<AuthStateError>());
final errorState = authCubit!.state as AuthStateError;
expect(errorState.errorMessage, contains('Exception'));
verify(mockAuthRepository.getTokenByCredentials('wrong@example.com', 'wrongpassword')).called(1);
expect(
authRepository.credentialPayloads.single,
{'email': 'wrong@example.com', 'password': 'wrongpassword'},
);
});
test('getTokenByCredentials - обработка различных типов ошибок', () async {
authCubit = AuthCubit(authRepository: mockAuthRepository);
when(mockAuthRepository.getTokenByCredentials(any, any))
.thenThrow(const FormatException('Invalid credentials format'));
authCubit = AuthCubit(authRepository: authRepository);
authRepository.error = const FormatException('Invalid credentials format');
final result = await authCubit!.getTokenByCredentials('invalid', 'pass');
@@ -111,27 +118,35 @@ void main() {
});
test('getTokenByCredentials - проверка вызова с правильными параметрами', () async {
authCubit = AuthCubit(authRepository: mockAuthRepository);
when(mockAuthRepository.getTokenByCredentials(any, any))
.thenAnswer((_) async => testToken);
authCubit = AuthCubit(authRepository: authRepository);
await authCubit!.getTokenByCredentials('user@test.com', 'securePassword123');
verify(mockAuthRepository.getTokenByCredentials('user@test.com', 'securePassword123')).called(1);
verifyNever(mockAuthRepository.getTokenByCredentials('other@test.com', 'otherPassword'));
expect(
authRepository.credentialPayloads.single,
{'email': 'user@test.com', 'password': 'securePassword123'},
);
expect(
authRepository.credentialPayloads.where((call) => call['email'] == 'other@test.com'),
isEmpty,
);
});
test('getTokenByCredentials - множественные вызовы', () async {
authCubit = AuthCubit(authRepository: mockAuthRepository);
when(mockAuthRepository.getTokenByCredentials(any, any))
.thenAnswer((_) async => testToken);
authCubit = AuthCubit(authRepository: authRepository);
await authCubit!.getTokenByCredentials('first@test.com', 'password1');
await Future.delayed(const Duration(milliseconds: 100));
await authCubit!.getTokenByCredentials('second@test.com', 'password2');
verify(mockAuthRepository.getTokenByCredentials('first@test.com', 'password1')).called(1);
verify(mockAuthRepository.getTokenByCredentials('second@test.com', 'password2')).called(1);
expect(authRepository.credentialPayloads.length, 2);
expect(
authRepository.credentialPayloads,
[
{'email': 'first@test.com', 'password': 'password1'},
{'email': 'second@test.com', 'password': 'password2'},
],
);
});
});
}

View File

@@ -2,24 +2,27 @@
import 'package:eatvio_app/app_keys.dart';
import 'package:eatvio_app/core/theme/theme_helper.dart';
import 'package:eatvio_app/features/auth/models/token_model.dart';
import 'package:eatvio_app/features/auth/presentation/bloc/auth_cubit/auth_cubit.dart';
import 'package:eatvio_app/features/auth/repositories/auth_repository.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'basic_widget_test.mocks.dart';
import '../../../../fixtures/doubles/auth_doubles.dart';
import '../../../../fixtures/fixture_reader.dart';
@GenerateMocks([AuthRepository])
void main() {
group('Auth Basic Widget Tests', () {
late MockAuthRepository mockAuthRepository;
late FixtureAuthRepository authRepository;
late AuthCubit authCubit;
late Map<String, dynamic> credentialsFixture;
setUp(() {
mockAuthRepository = MockAuthRepository();
authCubit = AuthCubit(authRepository: mockAuthRepository);
credentialsFixture = FixtureReader.readAsMap('auth/login_credentials.json');
authRepository = FixtureAuthRepository(
tokenFixture: TokenModel.fromJson(FixtureReader.readAsMap('auth/token_response.json')),
);
authCubit = AuthCubit(authRepository: authRepository);
});
tearDown(() {
@@ -38,7 +41,7 @@ void main() {
testWidgets('should create AuthCubit successfully', (WidgetTester tester) async {
// Arrange & Act
final cubit = AuthCubit(authRepository: mockAuthRepository);
final cubit = AuthCubit(authRepository: authRepository);
// Assert
expect(cubit, isA<AuthCubit>());
@@ -106,7 +109,7 @@ void main() {
await tester.pumpWidget(createTestWidget(emailWidget));
await tester.pumpAndSettle();
const testEmail = 'test@example.com';
final testEmail = credentialsFixture['email'] as String;
// Act
await tester.enterText(find.byKey(AppKeys.emailTextFieldAuthScreen), testEmail);
@@ -127,7 +130,7 @@ void main() {
await tester.pumpWidget(createTestWidget(passwordWidget));
await tester.pumpAndSettle();
const testPassword = 'password123';
final testPassword = credentialsFixture['password'] as String;
// Act
await tester.enterText(find.byKey(AppKeys.passwordTextFieldAuthScreen), testPassword);