I'm trying to write some unit tests for ApiBaseHelper methods (responsible for all HTTP requests).
class ApiBaseHelper {
String _baseUrl = "${Config.get('apiUrl')}/";
Dio _dio = Dio()..interceptors.add(DioCustomInterceptors());
ApiBaseHelper({Dio? dio}) {
_dio = dio ?? Dio()
..interceptors.add(DioCustomInterceptors());
_dio.options.baseUrl = _baseUrl;
}
Future<dynamic> get(String url, [Map<String, dynamic>? query]) async {
try {
Response<String> response =
await _dio.get(_baseUrl + url, queryParameters: query);
if (response.statusCode == 200) {
return response.data;
}
} on DioError catch (e) {
_returnException(e.response);
}
}
}
this is the file where I tried to test the GET method, I use the Dio package
import 'package:app/utils/services/network/api_base_helper.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http_mock_adapter/http_mock_adapter.dart';
const successMessage = {'message': 'Success'};
const errorMessage = {'message': 'error'};
const testPath = 'test';
const testData = {'data': 'sample data'};
const header = {'Content-Type': 'application/json'};
void main(){
WidgetsFlutterBinding.ensureInitialized() ;
final dio = Dio();
final dioAdapter = DioAdapter(dio: dio);
var baseUrl;
setUp(() {
dio.httpClientAdapter = dioAdapter;
baseUrl = 'https://example.com/';
});
group("", (){
test('Test Get', () async {
dioAdapter.onGet(
'$baseUrl$testPath',
(request) {
return request.reply(200, successMessage);
},
data: null,
queryParameters: {},
headers: {},
);
final service = ApiBaseHelper(
dio: dio,
);
final response = await service.get('test');
expect(response, successMessage);
});
});
}
when I run test I got this erro
package:flutter/src/services/platform_channel.dart 175:7 MethodChannel._invokeMethod
===== asynchronous gap ===========================
dart:async _asyncErrorWrapperHelper
package:dio/src/dio_mixin.dart 502:28 DioMixin.fetch._requestInterceptorWrapper.<fn>.<fn>.<fn>
package:dio/src/dio_mixin.dart 818:22 DioMixin.checkIfNeedEnqueue
package:dio/src/dio_mixin.dart 500:22 DioMixin.fetch._requestInterceptorWrapper.<fn>.<fn>
===== asynchronous gap ===========================
dart:async new Future
package:dio/src/dio_mixin.dart 499:13 DioMixin.fetch._requestInterceptorWrapper.<fn>
package:dio/src/dio_mixin.dart 494:14 DioMixin.fetch._requestInterceptorWrapper.<fn>
===== asynchronous gap ===========================
dart:async Future.then
package:dio/src/dio_mixin.dart 581:23 DioMixin.fetch.<fn>
dart:collection ListMixin.forEach
package:dio/src/dio_mixin.dart 577:18 DioMixin.fetch
package:dio/src/dio_mixin.dart 468:12 DioMixin.request
package:dio/src/dio_mixin.dart 55:12 DioMixin.get
package:app/utils/services/network/api_base_helper.dart 19:22 ApiBaseHelper.get
test/network/api_base_helper_test.dart 40:38 main.<fn>.<fn>
test/network/api_base_helper_test.dart 27:22 main.<fn>.<fn>
MissingPluginException(No implementation found for method read on channel plugins.it_nomads.com/flutter_secure_storage)
I'm using flutter secure storage to store user credential, but here I'm just trying to test ApiBaseHelper. any help please?