void main() {
late ApiManager apiManager;
late MockHTTPClient mockHTTPClient;
setUp(() {
mockHTTPClient = MockHTTPClient();
apiManager = ApiManager(mockHTTPClient);
});
test(
'given UserRepository class when getUser function is called and status code is 200 then a usermodel should be returned',
() async {
// Arrange
const mockResponse = '{"status": "200"}';
String url = "url";
when(() {
return mockHTTPClient.post(Uri.parse(url));
}).thenAnswer((invocation) => Future.delayed(Durations.short2,() => Response(mockResponse, 200),), );
// Act
final user = await apiManager.userLoginApi();
// Assert
expect(isuserExist, isA<Data()>());
},
);
Future<Data> userLoginApi({String? email, String? password}) async {
final response = await client.post(
Uri.parse('url'),
);
if (response.statusCode == 200) {
Map<String, String> body = {
"email": email.toString(),
"password": password.toString()
};
return Data.fromJson(body);
}
throw Exception('Some Error Occurred');
}
Getting this error ‘type ‘Null’ is not a subtype of type ‘Future.. Also, what should i pass in the body. It’s a user login api which includes email and pass. Since its a test case i cant pass the user credentials.
New contributor
Riot is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.