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

Generator create wrong code about GlobalKey. #389

Closed
nakajo2011 opened this issue Apr 20, 2021 · 4 comments
Closed

Generator create wrong code about GlobalKey. #389

nakajo2011 opened this issue Apr 20, 2021 · 4 comments
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

@nakajo2011
Copy link

nakajo2011 commented Apr 20, 2021

If target class has GlobalKey as field, the generator create wrong code. like this:

class _FakeGlobalKey<T extends State<StatefulWidget>> extends _i1.Fake
    implements _i2.GlobalKey<T> {}

State<StatefulWidget> should be has prefix _i2, but it was nothing.

version: 5.0.5

@srawlins
Copy link
Member

Hmm, I am unable to reproduce this in unit tests on 5.0.5... Can you show your @GenerateMocks annotation, and the import statements which should import State and GlobalKey?

@srawlins srawlins added the type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) label Apr 20, 2021
@nakajo2011
Copy link
Author

@srawlins Thanks for reply.

I try to generate mockito this Model class (but it is sample code).

import 'package:bccert_end_user_app/constants.dart';
import 'package:bccert_end_user_app/models/certificate.dart';
import 'package:bccert_end_user_app/providers/storage_provider.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:fluttertoast/fluttertoast.dart';

class Application with ChangeNotifier {
  final GlobalKey<NavigatorState> mainNavigator = GlobalKey<NavigatorState>();
  StorageProvider storageProvider = StorageProvider();
  List<Certificate> myCerts = <Certificate>[];
  Certificate? selectedCert;
  String? identifierCode;
  bool qrCodeFlag = false;

  static String get loginRouteName => '/login';

  static String get tabRouteName => '/tab';

  static String get signUpRouteName => '/sign_up';

  static String get certsRouteName => '/certs';

  static String get qrCodeRegistrationRouteName => '/qrcode_registration';

  static String get certOptionsRouteName => '/cert_options';

  static String get certDownloadRouteName => '/cert_download';

  static String get certDownloadSuccessRouteName => '/cert_download_success';

  static String get certDetailRouteName => '/cert_detail';

  static String get certVerifyRouteName => '/cert_verify';

  static String get certReceiversRouteName => '/cert_receivers';

  static String get confirmEmailRouteName => '/confirm_email';

  static String get profileRouteName => '/profile';

}

Annotation is bellow:

@GenerateMocks([Application])

I got bellow generated code:

// Mocks generated by Mockito 5.0.5 from annotations
// in bccert_end_user_app/test/viewmodels/cert_detail_view_model_test.dart.
// Do not manually edit this file.

import 'dart:ui' as _i7;

import 'package:bccert_end_user_app/models/application.dart' as _i4;
import 'package:bccert_end_user_app/models/certificate.dart' as _i6;
import 'package:bccert_end_user_app/providers/storage_provider.dart' as _i3;
import 'package:flutter/src/widgets/framework.dart' as _i2;
import 'package:flutter/src/widgets/navigator.dart' as _i5;
import 'package:mockito/mockito.dart' as _i1;

// ignore_for_file: comment_references
// ignore_for_file: unnecessary_parenthesis

class _FakeGlobalKey<T extends State<StatefulWidget>> extends _i1.Fake
    implements _i2.GlobalKey<T> {}

class _FakeStorageProvider extends _i1.Fake implements _i3.StorageProvider {}

/// A class which mocks [Application].
///
/// See the documentation for Mockito's code generation for more information.
class MockApplication extends _i1.Mock implements _i4.Application {
  MockApplication() {
    _i1.throwOnMissingStub(this);
  }

  @override
  _i2.GlobalKey<_i5.NavigatorState> get mainNavigator =>
      (super.noSuchMethod(Invocation.getter(#mainNavigator),
              returnValue: _FakeGlobalKey<_i5.NavigatorState>())
          as _i2.GlobalKey<_i5.NavigatorState>);
  @override
  _i3.StorageProvider get storageProvider =>
      (super.noSuchMethod(Invocation.getter(#storageProvider),
          returnValue: _FakeStorageProvider()) as _i3.StorageProvider);
  @override
  set storageProvider(_i3.StorageProvider? _storageProvider) =>
      super.noSuchMethod(Invocation.setter(#storageProvider, _storageProvider),
          returnValueForMissingStub: null);
  @override
  List<_i6.Certificate> get myCerts =>
      (super.noSuchMethod(Invocation.getter(#myCerts),
          returnValue: <_i6.Certificate>[]) as List<_i6.Certificate>);
  @override
  set myCerts(List<_i6.Certificate>? _myCerts) =>
      super.noSuchMethod(Invocation.setter(#myCerts, _myCerts),
          returnValueForMissingStub: null);
  @override
  set selectedCert(_i6.Certificate? _selectedCert) =>
      super.noSuchMethod(Invocation.setter(#selectedCert, _selectedCert),
          returnValueForMissingStub: null);
  @override
  set identifierCode(String? _identifierCode) =>
      super.noSuchMethod(Invocation.setter(#identifierCode, _identifierCode),
          returnValueForMissingStub: null);
  @override
  bool get qrCodeFlag =>
      (super.noSuchMethod(Invocation.getter(#qrCodeFlag), returnValue: false)
          as bool);
  @override
  set qrCodeFlag(bool? _qrCodeFlag) =>
      super.noSuchMethod(Invocation.setter(#qrCodeFlag, _qrCodeFlag),
          returnValueForMissingStub: null);
  @override
  bool get hasListeners =>
      (super.noSuchMethod(Invocation.getter(#hasListeners), returnValue: false)
          as bool);
  @override
  void addListener(_i7.VoidCallback? listener) =>
      super.noSuchMethod(Invocation.method(#addListener, [listener]),
          returnValueForMissingStub: null);
  @override
  void removeListener(_i7.VoidCallback? listener) =>
      super.noSuchMethod(Invocation.method(#removeListener, [listener]),
          returnValueForMissingStub: null);
  @override
  void dispose() => super.noSuchMethod(Invocation.method(#dispose, []),
      returnValueForMissingStub: null);
  @override
  void notifyListeners() =>
      super.noSuchMethod(Invocation.method(#notifyListeners, []),
          returnValueForMissingStub: null);
}

Additional Information

% flutter --version
Flutter 2.0.4 • channel stable • https://github.com/flutter/flutter.git
Framework • revision b1395592de (4 weeks ago) • 2021-04-01 14:25:01 -0700
Engine • revision 2dce47073a
Tools • Dart 2.12.2

@srawlins
Copy link
Member

OK I can reproduce this. Thanks much!

@srawlins srawlins self-assigned this Apr 27, 2021
@srawlins srawlins added the P2 A bug or feature request we're likely to work on label Apr 27, 2021
srawlins added a commit that referenced this issue Apr 28, 2021
Fixes #389

We already track type parameter bounds for classes being mocked. This change allows for classes which need to be Faked to be properly written as well, with import prefixes on imported types.

PiperOrigin-RevId: 370927872
@srawlins
Copy link
Member

Should be fixed in 5.0.7.

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