I would like to figure out how to modify the return value of a static getter for my unit tests in Flutter and Dart.
I'm unit testing a simple function:
Future<bool> exampleFunc() async {
if (Platform.isIOS) {
// Do some iOS-specific things
return false;
} else if (Platform.isAndroid) {
// Do some Android-specific things
return true; // just as an example
}
throw 'Unexpected platform';
}
And I would like to modify the return values of the static getters of a class: I would like to tweak Platform.isIOS
and Platform.isAndroid
return value for the different test cases.
Please note that I know of workarounds for this issue, for example, I could split the function into two functions (one for each platform), I could inject my own enum
(or in this case, even a bool
might work) that represents the supported platforms. In this example, I am actually going to do that, but I would like to know how to "modify" the return values of the Platform
class's getters, because sometimes, you don't want to modify the signature of a function, as others might depend on it and yet, you still want to unit test the function.