group('UserRepository', () {
late UserRepository repo;
late MockApiClient mockApi;
setUp(() {
mockApi = MockApiClient();
repo = UserRepository(mockApi);
});
test('getAll returns list of users', () async {
when(() => mockApi.get('/users')).thenAnswer(
(_) async => Response(
data: [{'id': 1, 'name': 'Ali'}],
statusCode: 200,
requestOptions: RequestOptions(),
),
);
final users = await repo.getAll();
expect(users.length, 1);
expect(users.first.name, 'Ali');
});
test('getAll throws on network error', () async {
when(() => mockApi.get('/users')).thenThrow(DioException(
type: DioExceptionType.connectionError,
requestOptions: RequestOptions(),
));
expect(() => repo.getAll(), throwsA(isA<NetworkException>()));
});
});