Created
June 7, 2025 05:06
-
-
Save rubywai/c0a3f80b16d612f299b1fdf17eaedb5d to your computer and use it in GitHub Desktop.
mock search api service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter_test/flutter_test.dart'; | |
| import 'package:mockito/mockito.dart'; | |
| import 'package:dio/dio.dart'; | |
| import 'package:flutter_bloc_weather/features/search/data/services/search_api_services.dart'; | |
| import 'mock.mocks.dart'; // Make sure this is generated from mock.dart | |
| void main() { | |
| group('SearchApiServices', () { | |
| late MockDio mockDio; | |
| late SearchApiServices api; | |
| setUp(() { | |
| mockDio = MockDio(); | |
| // ✅ Fix MissingStubError for dio.options | |
| when(mockDio.options).thenReturn(BaseOptions()); | |
| // ✅ Create API instance | |
| api = SearchApiServices(mockDio); | |
| }); | |
| test('returns CityModel on successful API call', () async { | |
| // ✅ Expected JSON response from server | |
| final mockJson = { | |
| "results": [ | |
| { | |
| "id": 1, | |
| "name": "Yangon", | |
| "latitude": 16.8, | |
| "longitude": 96.2, | |
| "country": "Myanmar" | |
| } | |
| ] | |
| }; | |
| // ✅ Return typed Response<Map<String, dynamic>> to satisfy Retrofit | |
| when(mockDio.fetch(any)).thenAnswer((invocation) async { | |
| final requestOptions = | |
| invocation.positionalArguments.first as RequestOptions; | |
| return Response<Map<String, dynamic>>( | |
| requestOptions: requestOptions, | |
| statusCode: 200, | |
| data: mockJson, | |
| ); | |
| }); | |
| // ✅ Perform the call | |
| final result = await api.searchCities(name: "Yangon", count: 1); | |
| // ✅ Verify result | |
| expect(result.results.length, 1); | |
| expect(result.results.first.name, "Yangon"); | |
| expect(result.results.first.country, "Myanmar"); | |
| }); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment