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

Mocks generated by @GenerateMocks do not implement secondary generic types for typed-mocks #410

Closed
bertiepinnock opened this issue May 17, 2021 · 1 comment
Assignees
Labels
P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@bertiepinnock
Copy link

Hi there,

I think I've noticed an issue with the @GenerateMocks annotation when it comes to applying secondary generic types (i.e. types of a type). For example the following class/mocks:

import 'package:mockito/annotations.dart';

@GenerateMocks([], customMocks: [MockSpec<Foo<Map<String, dynamic>>>()])
void main() {}

class Foo<T> {
  void doNothing(T arg) {}
}

Produces this as generated mock code:

// Mocks generated by Mockito 5.0.7 from annotations
// in project/test/test_mock.dart.
// Do not manually edit this file.

import 'package:mockito/mockito.dart' as _i1;

import 'test_mock.dart' as _i2;

// ignore_for_file: comment_references
// ignore_for_file: unnecessary_parenthesis

// ignore_for_file: prefer_const_constructors

// ignore_for_file: avoid_redundant_argument_values

/// A class which mocks [Foo].
///
/// See the documentation for Mockito's code generation for more information.
class MockFoo extends _i1.Mock implements _i2.Foo<Map> {
  MockFoo() {
    _i1.throwOnMissingStub(this);
  }

  @override
  void doNothing(Map<String, dynamic>? arg) =>
      super.noSuchMethod(Invocation.method(#doNothing, [arg]),
          returnValueForMissingStub: null);
}

This produces a compilation error on the MockFoo override of doNothing that states:
'MockFoo.doNothing' ('void Function(Map<String, dynamic>?)') isn't a valid override of 'Foo.doNothing' ('void Function(Map<dynamic, dynamic>)').dartinvalid_override

As far as I can tell, this is because when the MockFoo class is implementing Foo it is erasing the type parameters of the Map<String, dynamic> to just Map. If I manually change Foo<Map> to Foo<Map<String, dynamic>> it fixes the issue. I ran into this whilst mocking elements of the FlutterFire Firestore package which (in its current version) uses Map<String, dynamic> as a default type, so I can imagine it may cause problems for others too.

I did have a look over the documentation/issues and couldn't see anything open to cover this, but I won't be too surprised if I've just tried using something incorrectly.

Thanks!

@srawlins
Copy link
Member

Good catch; on the surface this definitely looks like a bug, but I'm surprised we don't have tests for this...

@srawlins srawlins added P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels May 17, 2021
@srawlins srawlins self-assigned this May 19, 2021
srawlins added a commit that referenced this issue May 24, 2021
…roperly.

Fixes #410

For example:

```dart
@GenerateMocks([], customMocks: [MockSpec<Foo<List<int>>>(as: #MockFooOfListOfInt)])
```

now generates:

```dart
class MockFooOfListOfInt extends _i1.Mock implements _i2.Foo<List<int>> { ... }
```

PiperOrigin-RevId: 374755599
srawlins added a commit that referenced this issue May 25, 2021
…roperly.

Fixes #410

For example:

```dart
@GenerateMocks([], customMocks: [MockSpec<Foo<List<int>>>(as: #MockFooOfListOfInt)])
```

now generates:

```dart
class MockFooOfListOfInt extends _i1.Mock implements _i2.Foo<List<int>> { ... }
```

PiperOrigin-RevId: 374755599
srawlins added a commit that referenced this issue May 25, 2021
…roperly.

Fixes #410

For example:

```dart
@GenerateMocks([], customMocks: [MockSpec<Foo<List<int>>>(as: #MockFooOfListOfInt)])
```

now generates:

```dart
class MockFooOfListOfInt extends _i1.Mock implements _i2.Foo<List<int>> { ... }
```

PiperOrigin-RevId: 374755599
srawlins added a commit that referenced this issue May 25, 2021
…roperly.

Fixes #410

For example:

```dart
@GenerateMocks([], customMocks: [MockSpec<Foo<List<int>>>(as: #MockFooOfListOfInt)])
```

now generates:

```dart
class MockFooOfListOfInt extends _i1.Mock implements _i2.Foo<List<int>> { ... }
```

PiperOrigin-RevId: 374755599
mosuem pushed a commit to dart-lang/test that referenced this issue Oct 17, 2024
…roperly.

Fixes dart-lang/mockito#410

For example:

```dart
@GenerateMocks([], customMocks: [MockSpec<Foo<List<int>>>(as: #MockFooOfListOfInt)])
```

now generates:

```dart
class MockFooOfListOfInt extends _i1.Mock implements _i2.Foo<List<int>> { ... }
```

PiperOrigin-RevId: 374755599
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

2 participants