Why do I get `MissingStubError` even though the function is stubbed?

110 views Asked by At

I have this function:

Future<void> run(
  void Function() onStart,
  void Function() newCallback,
  void Function() updateCallback,
  void Function() onFinish,
) {
  return Future(() async {
    // heavy work
  });
}

I am trying to mock it with:

when(() => module.run(
          () {},
          () {},
          () {},
          () {},
        )).thenAnswer(
      (invocation) => Future(() {}),
    );

Flutter test returns:

  MissingStubError: 'run'
  No stub was found which matches the arguments of this method call:
  run(Closure: () => void, Closure: () => void, Closure: () => void, Closure: () => void)
  
  Add a stub for this method using Mocktail's 'when' API.
  package:mocktail/src/mocktail.dart 30:5            _exceptionResponse.<fn>
  package:mocktail/src/mocktail.dart 131:37          Mock.noSuchMethod
  package:app/module.dart 94:16                      MockApiBindings.scan
  package:app/model.dart 165:20                      Model.startWork
  test/model_test.dart 168:9                         main.<fn>
  ===== asynchronous gap ===========================
  dart:async                                         _completeOnAsyncError
  test/model_test.dart 168:9                         main.<fn>

Why is this not working? Aren't all the empty closures returning void?

1

There are 1 answers

0
fdollack On

Duh...

when(() => module.run(
          any(),
          any(),
          any(),
          any(),
        )).thenAnswer(
      (invocation) => Future(() {}),
    );

That was unexpected, but makes sense...

If anyone finds another solution please let me know (: