I’m trying to mock a function which is having Future<bool> as return with
String` paramater.
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’m receiving following 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.
2
Answers
Its not a answer which I expected, but a workaround to make the test case run:
Here I changes the return type to
void
and used simple function for callback.Another solution to this problem is as below:
Here we will be able to return
Future<bool>
values in Mockito.