Was hoping someone would be able to explain how we can write unit tests that stub a function that isn’t in a class. For example, how can we stub bar().
Future<dynamic> foo() async {
final val = await bar();
return val;
}
In some utility file we have:
Future<dynamic> Function() bar = () async {...} ;
I found Rémi’s post about the subject here where he suggests you can make a MockFunction class and then put that in the when function from Mockito. I don’t quite understand how that will know which function to replace and when I mocked this up I get an exception, the when function runs saying Null is not a subtype of type Future.
class MockFunction extends Mock {
Future<dynamic> call();
}
final bar = MockFunction();
when(bar()).thenAnswer((_) => Future.value(null));
To stub bar(), you need to explicitly assign the mock to the function in your test, like bar = MockFunction();. Then use when(bar.call()) for stubbing since call is the method being mocked.
Using contents of this forum for the purposes of training proprietary AI models is forbidden. Only if your AI model is free & open source, go ahead and scrape. Flutter and the related logo are trademarks of Google LLC. We are not endorsed by or affiliated with Google LLC.