Skip to content

Commit

Permalink
Do not generate a fake for a class which is only used as a nullable t…
Browse files Browse the repository at this point in the history
…ype in a Future.

Fixes #409

Other containers do not feature this problem, like List, Stream, etc, as they are allowed to be empty. A Future can only be empty in this nullable type case.

PiperOrigin-RevId: 374242451
  • Loading branch information
srawlins committed May 19, 2021
1 parent 6de2746 commit f50bec6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Support mocking methods with typed_data List return types.
* Support mocking methods with return types declared in private SDK libraries
(such as HttpClient and WebSocket, declared in `dart:_http`).
* Do not generate a fake for a class which is only used as a nullable type in a
Future. [#409](https://github.com/dart-lang/mockito/issues/409)

## 5.0.7

Expand Down
8 changes: 6 additions & 2 deletions lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -960,10 +960,14 @@ class _MockLibraryInfo {
} else if (type.isDartCoreDouble) {
return literalNum(0.0);
} else if (type.isDartAsyncFuture || type.isDartAsyncFutureOr) {
var typeArgument = typeArguments.first;
final typeArgument = typeArguments.first;
final futureValueArguments =
typeSystem.isPotentiallyNonNullable(typeArgument)
? [_dummyValue(typeArgument)]
: <Expression>[];
return _futureReference(_typeReference(typeArgument))
.property('value')
.call([_dummyValue(typeArgument)]);
.call(futureValueArguments);
} else if (type.isDartCoreInt) {
return literalNum(0);
} else if (type.isDartCoreIterable) {
Expand Down
18 changes: 17 additions & 1 deletion test/builder/auto_mocks_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ void main() {
'''),
_containsAllOf(dedent2('''
_i3.Future<void> m() => (super.noSuchMethod(Invocation.method(#m, []),
returnValue: Future<void>.value(null),
returnValue: Future<void>.value(),
returnValueForMissingStub: Future.value()) as _i3.Future<void>);
''')),
);
Expand Down Expand Up @@ -1836,6 +1836,22 @@ void main() {
);
});

test('creates dummy non-null return values for Futures of nullable types',
() async {
await expectSingleNonNullableOutput(
dedent('''
class Bar {}
class Foo {
Future<Bar?> m() async => null;
}
'''),
_containsAllOf(dedent2('''
_i3.Future<_i2.Bar?> m() => (super.noSuchMethod(Invocation.method(#m, []),
returnValue: Future<_i2.Bar?>.value()) as _i3.Future<_i2.Bar?>);
''')),
);
});

test(
'creates dummy non-null return values for Futures of known typed_data classes',
() async {
Expand Down

0 comments on commit f50bec6

Please sign in to comment.