I am trying to test some api call function:
Future<SomeType> getType(int id) async {
String token = await retrieveToken();
String api = await getApiEndpoint();
final response = await client.get("$api/something/$id", headers: { "Authorization": "Bearer $token"});
if (response.statusCode == 200) {
String body = utf8.decode(response.bodyBytes);
return SomeType.fromJson(json.decode(body));
} else {
throw Exception('Failed to load some types from API');
}
}
The two functions:
retrieveToken();
getApiEndpoint();
that are called inside are not members of the same class as getType() function. They also contain inside them instances of FlutterSecureStorage().
I don't need to test this function, I just want to mock their responses in the test of getType function.
I've created following test example:
class MockedApiEndpoint extends Mock implements ApiEndpoint {}
void main() {
WidgetsFlutterBinding.ensureInitialized();
test('returns SomeType', () async {
final typesRepository = TypesRepository();
final mockedApiEndpoint = MockedApiEndpoint();
when(mockedApiEndpoint.getApiEndpoint()).thenAnswer((_) async => Future.value("some_url.com"));
when(mockedApiEndpoint.retrieveToken()).thenAnswer((_) async => Future.value("some_token"));
typesRepository.client = MockClient( (request) async {
final mapJson = {'id' : 123};
return Response(json.encode(mapJson), 200);
});
final type = await typesRepository.getType(123);
expect(type.id, 123);
});
}
I was able to succesfully Mock the client reposne, I am also trying to mock responses of those two functions, but I am constantly receiveing following error:
MissingPluginException(No implementation found for method read on channel plugins.it_nomads.com/flutter_secure_storage)
I would be very greatful for any hints how to approach this topic and what I am doing wrong with this one. I am new to unit testing in Flutter, I've tried to find any similar problem to mine on the web but unfortunatelly failed.
Aucun commentaire:
Enregistrer un commentaire