skip to Main Content

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


  1. Chosen as BEST ANSWER

    Its not a answer which I expected, but a workaround to make the test case run:

          final void Function(String) _handleFilterSelect = MockFunction();
          FilterList filterList = new FilterList(
            '/type/movie',
            onFilterSelect: (String p) async {
              _handleFilterSelect(p);
              return true;
            },
          );
          await widgetTester.pumpWidget(MaterialApp(
            home: Scaffold(
              body: filterList,
            ),
          ));
          await widgetTester.pumpAndSettle();
          final filtersFinder = find.byType(ListTile);
          expect(filtersFinder, findsOneWidget);
          await widgetTester.tap(filtersFinder);
          await widgetTester.pumpAndSettle();
          verify(_handleFilterSelect('/view/movie_1234')).called(1);
       
    # outside test case:
    class MockFunction extends Mock {
      void call(String param);
    }
    
    

    Here I changes the return type to void and used simple function for callback.


  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search