dart-lang / mockito

Mockito-inspired mock library for Dart
https://pub.dev/packages/mockito
Apache License 2.0
631 stars 162 forks source link

Cannot generate mock of class with mixin that overrides operator #437

Open 0biWanKenobi opened 3 years ago

0biWanKenobi commented 3 years ago

I am using Mockito 5.0.10, and I do have read about various fixes regarding operators override:

However, I'm still having issues when using EquatableMixinfrom the package Equatable.

I have a class like this:

class AuctionLot {
   final int fie;   
}

class AuctionLotDetail extends Foo with EquatableMixin{
 final int fee;
}

EquatableMixin overrides the equality operator, and Mockito doesn't seem to handle that well:

test/mocks_repository.mocks.dart:53:7: Error: The implementation of '==' in the non-abstract class '_FakeAuctionLotDetail' does not conform to its interface.
class _FakeAuctionLotDetail extends _i1.Fake implements _i5.AuctionLotDetail {}
      ^^^^^^^^^^^^^^^^^^^^^
org-dartlang-sdk:///third_party/dart/sdk/lib/_internal/vm/lib/object_patch.dart:21:27: Context: The parameter 'other' of the method 'Object.==' has type 'Object', which does not match the corresponding type, 'Object?', in the overridden method, 'AuctionLot with EquatableMixin.=='.
 - 'Object' is from 'dart:core'.
Change to a supertype of 'Object?', or, for a covariant parameter, a subtype.
  bool operator ==(Object other) native "Object_equals";
                          ^
lib/model/auction_lot_detail.dart:8:7: Context: This is the overridden method ('==').
class AuctionLotDetail extends AuctionLot with EquatableMixin {
      ^
test/mocks_repository.mocks.dart:101:7: Error: The implementation of '==' in the non-abstract class 'MockAuctionLotDetail' does not conform to its interface.
class MockAuctionLotDetail extends _i1.Mock implements _i5.AuctionLotDetail {
      ^^^^^^^^^^^^^^^^^^^^
/C:/src/flutter_2.0.6/.pub-cache/hosted/pub.dartlang.org/mockito-5.0.10/lib/src/mock.dart:196:20: Context: The parameter 'other' of the method 'Mock.==' has type 'Object', which does not match the corresponding type, 'Object?', in the overridden method, 'AuctionLot with EquatableMixin.=='.
 - 'Object' is from 'dart:core'.
Change to a supertype of 'Object?', or, for a covariant parameter, a subtype.

The generated Fakes:

class _FakeAuctionLotDetail extends _i1.Fake implements _i4.AuctionLotDetail {}

The generated Mock:

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

[.. all the prop overrides omitted for brevity]

  @override
  String toString() => super.toString();
}
srawlins commented 3 years ago

The workaround is to upgrade to equatable 2.0.3 which uses non-nullable Object as the parameter to ==.

0biWanKenobi commented 3 years ago

I already use equatable 2.0.3, but that hasn't helped