This is a method I was trying to write a test for. I just want to write a very simple test that would just verify that send() was invoked.
But the main trouble maker is the instance MultipartRequest.
As you can see, I am directly using the MultipartRequest instance, not as a dependency. So, I don't think I am supposed to stub it.
I can easily stub for getCachedToken() method as an instance of TokenValueLocalDataSource was passed in as a dependency.
What do you think I should do to get my desired test result?
Future<Unit> addPost({
File? imageFile,
String? bodyText,
}) async {
if (imageFile != null || bodyText != null) {
final tokenModel = await tokenValueLocalDataSource.getCachedToken();
final stringUrl = EnvValues.addPostUrl;
final request = http.MultipartRequest(
'POST',
Uri.parse(stringUrl),
)..headers.addAll(
{
'Authorization': 'Token ${tokenModel.token}',
},
);
if (bodyText != null) {
request.fields['body'] = bodyText;
}
if (imageFile != null) {
request.files.add(
await http.MultipartFile.fromPath('image', imageFile.path),
);
}
final response = await request.send(); // This needs to verified.
if (response.statusCode == 200) {
return unit;
} else {
throw ServerException();
}
} else {
throw InvalidRequestException(
"Both imageFile and bodyText can't be null",
);
}
}