I have a helper class (in Dart) like below:
class AppInfoHelper {
Future<String> getAppName() async {
return getPackageInfo().then((info) => info.appName);
}
Future<String> getAppVersion() async {
return getPackageInfo().then((info) => info.version);
}
Future<String> getBuildNumber() async {
return getPackageInfo().then((info) => info.buildNumber);
}
Future<PackageInfo> getPackageInfo() async => PackageInfo.fromPlatform();
}
The responsibility of this class is providing app info. I'm using PackageInfo as one of the library. And for some reasons, I won't supply PackageInfo as constructor parameter.
How can I write proper unit test for this class? I'm not sure how to do stubbing since we getPackageInfo() method is coming from class under test itself.
Note:
I'm using mockito (https://pub.dev/packages/mockito) and some reference mentioned about doing something like spy, but I don't think it's available from Dart/Flutter mockito.
First, if you won't supply an instance of
PackageInfoto your class then you'll have to create your ownMockAppInfoHelperthat will let you use a fakePackageInfoclass.Here's an example of implementation:
And to mock a fake
PackageInfoclass you'll have to generate some mocks by adding theGenerateMocksannotation and runningbuild_runner:Now you can start to write your test method, here's an example of a test for
getAppName: