Skip to content

False generation of mocks containing generated entities #345

Open
@NikoBoerger

Description

@NikoBoerger

Hi,

I'm having the following simple class and want to generate a mock for it:

class PlayerModelConverter {
  PlayerTableCompanion convertToPlayerCompanion(Player player);

  Player convertToPlayer(PlayerModel playerModel);
}

"PlayerTableCompanion" and "PlayerModel" are generated classes from the Moor library.
When mocking with @GenerateMocks([PlayerModelConverter]), mockito creates the following:

class MockPlayerModelConverter extends _i1.Mock
    implements _i4.PlayerModelConverter {
  MockPlayerModelConverter() {
    _i1.throwOnMissingStub(this);
  }

  @override
  dynamic convertToPlayerCompanion(_i2.Player? player) => super
      .noSuchMethod(Invocation.method(#convertToPlayerCompanion, [player]));
  @override
  _i2.Player convertToPlayer(dynamic playerModel) =>
      (super.noSuchMethod(Invocation.method(#convertToPlayer, [playerModel]),
          returnValue: _FakePlayer()) as _i2.Player);
}

Both of the moor classes are converted to "dynamic", which obviously doesn't work.

The generated classes from moor look like this:

class PlayerModel extends DataClass implements Insertable<PlayerModel> {
  final int id;
  final String name;
  final String? imagePath;
  final KeyboardVariant? keyboardVariant;
  final ShortcutButtons? shortcutButtons;
  final bool? showCheckoutHints;
  PlayerModel(
      {required this.id,
      required this.name,
      this.imagePath,
      this.keyboardVariant,
      this.shortcutButtons,
      this.showCheckoutHints});
  factory PlayerModel.fromData(Map<String, dynamic> data, GeneratedDatabase db,
      {String? prefix}) {
    final effectivePrefix = prefix ?? '';
    final intType = db.typeSystem.forDartType<int>();
    final stringType = db.typeSystem.forDartType<String>();
    final boolType = db.typeSystem.forDartType<bool>();
    return PlayerModel(
      id: intType.mapFromDatabaseResponse(data['${effectivePrefix}id'])!,
      name: stringType.mapFromDatabaseResponse(data['${effectivePrefix}name'])!,
      imagePath: stringType
          .mapFromDatabaseResponse(data['${effectivePrefix}image_path']),
      keyboardVariant: $PlayerTableTable.$converter0.mapToDart(intType
          .mapFromDatabaseResponse(data['${effectivePrefix}keyboard_variant'])),
      shortcutButtons: $PlayerTableTable.$converter1.mapToDart(stringType
          .mapFromDatabaseResponse(data['${effectivePrefix}shortcut_buttons'])),
      showCheckoutHints: boolType.mapFromDatabaseResponse(
          data['${effectivePrefix}show_checkout_hints']),
    );
  }

[....]

Seems like a normal class to me. Is there a reason why this isn't working? Am I doing something wrong? The code generation works fine for my other mocks, that don't have dependencies to the generated classes from moor.

==============================
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.0.1, on macOS 11.2.2 20D80 darwin-x64, locale de-DE)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.1)
[✓] Connected device (1 available)

Flutter 2.0.1 • channel stable • https://github.com/flutter/flutter.git
Framework • revision c5a4b4029c (30 hours ago) • 2021-03-04 09:47:48 -0800
Engine • revision 40441def69
Tools • Dart 2.12.0

mockito: ^5.0.0-nullsafety.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requesttype-documentationA request to add or improve documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions