Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returning a Future<ExtensionType> generates invalid code #801

Open
xvrh opened this issue Feb 14, 2025 · 1 comment · May be fixed by #802
Open

Returning a Future<ExtensionType> generates invalid code #801

xvrh opened this issue Feb 14, 2025 · 1 comment · May be fixed by #802

Comments

@xvrh
Copy link

xvrh commented Feb 14, 2025

import 'package:mockito/annotations.dart';

class Foo {
  Future<Id> bar() async => Id(0);
}

extension type Id(int id) {}

@GenerateNiceMocks([MockSpec<Foo>()])
class Test {}

Will generate

class MockFoo extends _i1.Mock implements _i2.Foo {
  @override
  _i3.Future<_i2.Id> bar() =>
      (super.noSuchMethod(
            Invocation.method(#bar, []),
            returnValue: _i3.Future<_i2.Id>.value(0),
            returnValueForMissingStub: _i3.Future<_i2.Id>.value(0),
          )
          as _i3.Future<_i2.Id>);
}

and error: The argument type 'int' can't be assigned to the parameter type 'FutureOr<Id>?'. (argument_type_not_assignable at [mock] test/mock.mocks.dart:34)

@xvrh
Copy link
Author

xvrh commented Feb 14, 2025

Workaround is to specify a fallbackGenerator:

@GenerateNiceMocks([
  MockSpec<Foo>(fallbackGenerators: {#bar: fallback}),
])
class Test {}

Id fallback() => Id(0);

Still, that could be interesting to generate something working by default?

@xvrh xvrh linked a pull request Feb 20, 2025 that will close this issue
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant