felangel / mocktail

A mock library for Dart inspired by mockito
https://pub.dev/packages/mocktail
MIT License
617 stars 81 forks source link

Mocking generic method multiple times with different type arguments fails #116

Closed hkuczynski closed 2 years ago

hkuczynski commented 2 years ago

Describe the bug When I mock a generic method that is expected to return an object of a class with a generic type, subsequent calls to when ... thenReturn overwrite the previous ones.

To Reproduce Run this test:

import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';

class Box<T> {}

class Store {
  Box<T> getBox<T>() {
    return Box<T>();
  }
}

class MockStore extends Mock implements Store {}

void main() {
  test('simple test', () {
    final store = MockStore();
    final intBox = Box<int>();
    final boolBox = Box<bool>();
    when(() => store.getBox<int>()).thenReturn(intBox);
    when(() => store.getBox<bool>()).thenReturn(boolBox);

    final crash = store.getBox<int>();
  });
}

Expected behavior

    when(() => store.getBox<int>()).thenReturn(intBox);
    when(() => store.getBox<bool>()).thenReturn(boolBox);

    // should return intBox
    store.getBox<int>(); 

    // should return boolBox
    store.getBox<bool>(); 

Screenshots

CleanShot 2022-03-01 at 14 41 13@2x

Logs Paste the output of running flutter doctor -v here.

[✓] Flutter (Channel stable, 2.8.1, on macOS 12.1 21C52 darwin-arm, locale en-PL)
    • Flutter version 2.8.1 at /Users/hubertkuczynski/fvm/versions/2.8.1
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 77d935af4d (3 months ago), 2021-12-16 08:37:33 -0800
    • Engine revision 890a5fca2e
    • Dart version 2.15.1

[✓] Android toolchain - develop for Android devices (Android SDK version 32.0.0)
    • Android SDK at /Users/hubertkuczynski/Library/Android/sdk
    • Platform android-32, build-tools 32.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.11.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2020.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)

[✓] VS Code (version 1.64.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.34.0

[✓] Connected device (1 available)
    • Chrome (web) • chrome • web-javascript • Google Chrome 98.0.4758.109

• No issues found!
felangel commented 2 years ago

Hi @hkuczynski 👋 Thanks for opening an issue!

I'm unable to reproduce the problem using Dart 2.16.1 (stable) and mocktail 0.3.0-dev.1. Are you using mocktail 0.1.0-dev.1?

hkuczynski commented 2 years ago

I was using 0.2.0. I've just tested on 0.3.0 and it works fine. Thanks for your help!