I'm trying to mock a function which is having Future<bool> as return with
String` parameter.
I have defined the following test:
final Future<bool> Function(String) _handleFilterSelect = MockFunction();
when(_handleFilterSelect.call('/view/movie_1234'))
.thenAnswer((_) => Future.value(true));
FilterList filterList = new FilterList(
'/view/movie',
onFilterSelect: _handleFilterSelect,
);
await widgetTester.pumpWidget(MaterialApp(
localizationsDelegates: delegate,
home: Scaffold(
body: filterList,
),
theme: ThemeUtil().getCurrentTheme()!.defaultThemeData,
));
await widgetTester.pumpAndSettle();
final filtersFinder = find.byType(ListTile);
await widgetTester.tap(filtersFinder);
await widgetTester.pumpAndSettle();
verify(_handleFilterSelect('/view/movie_1234')).called(1);
## Outside these tests:
class MockFunction extends Mock {
Future<bool> call(String param);
}
But I get this error:
The following _TypeError was thrown running a test: type 'Null' is not a subtype of type 'Future'
What is correct way of implementing _handleFilterSelect
for Future
return values? The test works if the function is non-future function.
Another solution to this problem is as below:
final Future<bool> Function(String) _handleFilterSelect =
MockFunction().call;
when(_handleFilterSelect('/view/movie_1234'))
.thenAnswer((_) => Future.value(true));
FilterList filterList = new FilterList(
'/type/movie',
onFilterSelect: _handleFilterSelect,
);
....
# Mock function will look like:
class MockFunction extends Mock {
Future<bool> call(String param) {
return super.noSuchMethod(Invocation.method(#call, [param]),
returnValue: Future.value(false)) as Future<bool>;
}
}
Here we will be able to return Future<bool>
values in Mockito.