Missing stub error on Mockito in Flutter. Trying to use post of Dio
I think the problem was in the “FormData” instance in the prod function:
var formData = FormData.fromMap({
'profileImage': await MultipartFile.fromFile(imagePath),
});
Every time the test runs, a new instance is created and the stub is not the same.
So the solution I found, is to use a type matcher to check if any parameter of type 'FormData' is called with the function.
test('should verify if post method is successful called', () async {
httpStubs.setUpMockHttpClientAppSuccess(expectedUrl, formData);
await dataSource.setUserProfileImage(
email: email,
imagePath: imagePath,
);
verify(
() => httpClientApp.post(
expectedUrl,
body: isA<FormData>(),
),
).called(1);
});
The stub function:
void setUpMockHttpClientAppSuccess(String url, dynamic payload) {
when(
() => httpClientApp.post(
any(),
body: any(named: 'body'),
),
).thenAnswer(
(_) async => Response(
data: null,
statusCode: 200,
requestOptions: CommomMocks.getRequestOptions(url),
),
);
}
I'm not sure if the solution is the best or the best practice, therefore this resolves my test problem. If someone knows a better solution, please tell me.
Thanks to all!! :)