I'm trying to fetch categories data from API which works as expected. When I am trying to test it and I am running into the following error
type '_InternalLinkedHashMap' is not a subtype of type 'List'
package:test_flutter/service.dart 32:12 CategoryService.getCats
service.dart
class Service {
String apiUrl = "...";
http.Client _client;
service() {
_client = IOClient();
}
Service.customClient(this._client);
Future<List<Categories>> getCats() async {
final response = await _client.get(
apiUrl + "/categories",
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer $token',
},
);
if(response.statusCode == 200 || response.statusCode == 201) {
print('Response status: ${response.statusCode}');
print('Response body: ${response.body}');
List jsonList = json.decode(response.body);
List<Categories> catList = [];
for (var json in jsonList) {
Categories temp = Categories.fromJson(json);
print (temp);
catList.add(temp);
}
return catList;
} else {
print('Response status: ${response.statusCode}');
throw Exception("error getting categories");
}
}
}
model.dart
class Categories {
String _id;
String _name;
Categories(String id, String name) {
this._id = id;
this._name = name;
}
factory Categories.fromJson(Map<String, dynamic> json) {
return Categories.full(
json['id'], json['name']);
}
String getId() {
return _id;
}
void setId(String id) {
this._id = id;
}
String getName() {
return _name;
}
void setName(String name) {
this._name = name;
}
}
category_test.dart
class MockClient extends Mock implements http.Client {}
main() {
group('test categories', ()
{
test(
'returns list of categories if the http call completes successfully', () async {
final client = MockClient();
when(client.get('....',
headers: anyNamed('headers'),
)
).thenAnswer((_) async {
return http.Response(
'{"id":"123","name":"Health"}',
200);
});
List<Categories> value = await Service.customClient(client).getCats();
expect(value, const TypeMatcher<List<Categories>>());
expect(value[1].getId, "123");
expect(value[1].getName, "Health");
});
});
}
I am not exactly sure why I am getting this error.I need to write the test files and Im not sure what I am doing wrong.
Aucun commentaire:
Enregistrer un commentaire