refactor(tests): clean up profile_repository_test and enhance error handling in API calls
This commit is contained in:
@@ -1,3 +1,7 @@
|
|||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
import 'package:mockito/annotations.dart';
|
||||||
|
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
import 'package:mockito/annotations.dart';
|
import 'package:mockito/annotations.dart';
|
||||||
@@ -6,12 +10,13 @@ import 'package:recipe_flutter/core/api/api_client.dart';
|
|||||||
import 'package:recipe_flutter/core/api/api_exception.dart';
|
import 'package:recipe_flutter/core/api/api_exception.dart';
|
||||||
import 'package:recipe_flutter/features/profile/data/profile_repository.dart';
|
import 'package:recipe_flutter/features/profile/data/profile_repository.dart';
|
||||||
|
|
||||||
// Generate a MockRef class for the Ref type.
|
// Detta genererar MockRef i en separat fil
|
||||||
@GenerateMocks([Ref])
|
@GenerateMocks([Ref])
|
||||||
void main() {}
|
import 'profile_repository_test.mocks.dart';
|
||||||
|
void main() {
|
||||||
|
|
||||||
class MockApiClient extends Mock implements ApiClient {}
|
class MockApiClient extends Mock implements ApiClient {}
|
||||||
|
late ProfileRepository profileRepository;
|
||||||
void main() {
|
void main() {
|
||||||
group('ProfileRepository', () {
|
group('ProfileRepository', () {
|
||||||
late ProfileRepository profileRepository;
|
late ProfileRepository profileRepository;
|
||||||
@@ -19,7 +24,7 @@ void main() {
|
|||||||
|
|
||||||
setUp(() {
|
setUp(() {
|
||||||
mockApiClient = MockApiClient();
|
mockApiClient = MockApiClient();
|
||||||
final mockRef = MockRef(); // Ensure MockRef is generated by Mockito
|
final mockRef = MockRef();
|
||||||
profileRepository = ProfileRepository(mockApiClient, mockRef);
|
profileRepository = ProfileRepository(mockApiClient, mockRef);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -34,8 +39,8 @@ void main() {
|
|||||||
verify(mockApiClient.getJson('/api/profile')).called(1);
|
verify(mockApiClient.getJson('/api/profile')).called(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should throw ApiException when API call fails', () async {, type: ApiErrorType.server
|
test('should throw ApiException when API call fails', () async {
|
||||||
when(mockApiClient.getJson('/api/profile')).thenThrow(ApiException(message: 'Failed to fetch profile'));
|
when(mockApiClient.getJson('/api/profile')).thenThrow(ApiException(message: 'Failed to fetch profile', type: ApiErrorType.server));
|
||||||
|
|
||||||
expect(() => profileRepository.getProfile(), throwsA(isA<ApiException>()));
|
expect(() => profileRepository.getProfile(), throwsA(isA<ApiException>()));
|
||||||
verify(mockApiClient.getJson('/api/profile')).called(1);
|
verify(mockApiClient.getJson('/api/profile')).called(1);
|
||||||
@@ -57,7 +62,6 @@ void main() {
|
|||||||
test('should throw ApiException when API call fails', () async {
|
test('should throw ApiException when API call fails', () async {
|
||||||
final profileData = {'username': 'newuser', 'email': 'new@example.com'};
|
final profileData = {'username': 'newuser', 'email': 'new@example.com'};
|
||||||
when(mockApiClient.patchJson(any, profileData)).thenThrow(ApiException(message: 'Failed to update profile', type: ApiErrorType.server));
|
when(mockApiClient.patchJson(any, profileData)).thenThrow(ApiException(message: 'Failed to update profile', type: ApiErrorType.server));
|
||||||
|
|
||||||
expect(() => profileRepository.updateProfile(profileData), throwsA(isA<ApiException>()));
|
expect(() => profileRepository.updateProfile(profileData), throwsA(isA<ApiException>()));
|
||||||
verify(mockApiClient.patchJson(any, profileData)).called(1);
|
verify(mockApiClient.patchJson(any, profileData)).called(1);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user