diff --git a/lib/src/builder.dart b/lib/src/builder.dart index 7f1f310a..9ca4f90e 100644 --- a/lib/src/builder.dart +++ b/lib/src/builder.dart @@ -1866,7 +1866,7 @@ class _MockClassInfo { _dummyFakedValue(dartType, invocation), ExtensionTypeElement2(:final typeErasure) when !typeErasure.containsPrivateName => - _dummyValue(typeErasure, invocation), + _dummyValue(typeErasure, invocation).asA(_typeReference(dartType)), ExtensionTypeElement2() => _dummyValueFallbackToRuntime( dartType, invocation, diff --git a/test/builder/auto_mocks_test.dart b/test/builder/auto_mocks_test.dart index 76543f75..c49105c3 100644 --- a/test/builder/auto_mocks_test.dart +++ b/test/builder/auto_mocks_test.dart @@ -4180,14 +4180,26 @@ void main() { test('are supported as return types', () async { await expectSingleNonNullableOutput( - dedent(''' + dedent(''' extension type E(int v) {} class Foo { E get v; } '''), - decodedMatches(allOf(contains('E get v'), contains('returnValue: 0'))), - ); + decodedMatches(allOf( + contains('E get v'), contains('returnValue: (0 as _i2.E)')))); + }); + + test('are supported as Future return types', () async { + await expectSingleNonNullableOutput( + dedent(''' + extension type E(int v) {} + class Foo { + Future get v; + } + '''), + decodedMatches(allOf(contains('_i3.Future<_i2.E> get v'), + contains('returnValue: _i3.Future<_i2.E>.value((0 as _i2.E))')))); }); }); group('build_extensions support', () { diff --git a/test/end2end/foo.dart b/test/end2end/foo.dart index 09f676fe..5beda0a0 100644 --- a/test/end2end/foo.dart +++ b/test/end2end/foo.dart @@ -74,4 +74,7 @@ class UsesExtTypes { Ext extTypeReturn(int _) => Ext(42); bool privateExtTypeArg(ExtOfPrivate _) => true; ExtOfPrivate privateExtTypeReturn(int _) => ExtOfPrivate(private); + Future futureExtTypeReturn(int _) => Future.value(Ext(43)); + Future futureExtOfPrivateTypeReturn(int _) => + Future.value(ExtOfPrivate(private)); } diff --git a/test/end2end/generated_mocks_test.dart b/test/end2end/generated_mocks_test.dart index ca891e9b..2de83aa4 100644 --- a/test/end2end/generated_mocks_test.dart +++ b/test/end2end/generated_mocks_test.dart @@ -365,6 +365,15 @@ void main() { expect(usesExtTypes.extTypeReturn(2), equals(Ext(42))); expect(usesExtTypes.extTypeReturn(42), equals(Ext(0))); }); + + test('a method using extension type as a return type can be stubbed', + () async { + when(usesExtTypes.futureExtTypeReturn(2)) + .thenAnswer((_) => Future.value(Ext(42))); + expect(await usesExtTypes.futureExtTypeReturn(2), equals(Ext(42))); + expect(await usesExtTypes.futureExtTypeReturn(42), equals(Ext(0))); + expect(await usesExtTypes.futureExtTypeReturn(42), equals(0)); + }); }); });