felangel / mocktail

A mock library for Dart inspired by mockito
https://pub.dev/packages/mocktail
MIT License
588 stars 80 forks source link

Stubbing Future<void> Example from README does not compile #195

Closed JosiahMendes closed 1 year ago

JosiahMendes commented 1 year ago

Describe the bug The example for solving Why am I seeing error: type 'Null' is not a subtype of type 'Future'? in the Mocktail README.md does not appear to compile.

To Reproduce

import 'package:test/test.dart';
import 'package:mockito/mockito.dart';

class Person {
  Future<void> sleep() async {
    await Future<void>.delayed(const Duration(hours: 8));
  }
}

class MockPerson extends Mock implements Person {}

void main() {
  test("test case", () async {
    final person = MockPerson()
    // LINT ERROR here The argument type 'Future<Null> Function(Invocation)' can't be assigned to the parameter type 'Future<void> Function() Function(Invocation)'.dart[argument_type_not_assignable](https://dart.dev/diagnostics/argument_type_not_assignable)
    when(() => person.sleep()).thenAnswer((_) async {});

    await person.sleep();
    verify(() => person.sleep()).called(1);
  });
}

Logs Dart Analyze:

Analyzing test.dart...                 0.9s

  error • test.dart:15:43 • The argument type 'Future<Null> Function(Invocation)' can't be assigned to the parameter type 'Future<void> Function() Function(Invocation)'. •
          argument_type_not_assignable
   info • test.dart:1:8 • The imported package 'test' isn't a dependency of the importing package. Try adding a dependency for 'test' in the 'pubspec.yaml' file. •
          depend_on_referenced_packages

2 issues found.
Dart SDK version: 2.19.6 (stable) (Tue Mar 28 13:41:04 2023 +0000) on "macos_x64"

Additional context Add any other context about the problem here.

JosiahMendes commented 1 year ago

Replacing the when method previously defined as:

when(() => person.sleep()).thenAnswer((_) async {});

with:

    when(() => person.sleep()).thenAnswer((_) => () async {});

solves the lint issue, but causes a runtime failure -

Bad state: No method stub was called from within `when()`. Was a real method called, or perhaps an extension method?

Thanks in advance!

JosiahMendes commented 1 year ago

Stupid error - confused two packages with a similar API. Using the right import fixes it