dart-lang / mockito

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

Mocking of Dependencies won't work correctly #435

Closed MalteBoehm closed 3 years ago

MalteBoehm commented 3 years ago

Hey guys, I was trying to mock a controller, service, and the HTTP client

However, only the HTTP client worked and both other mocks always fail.

Anything i´m doing wrong?

Thanks for your help

testing class

@GenerateMocks([http.Client, LoginService, LoginController])
void main() {
  enableFlutterDriverExtension();
  final MealService mealService = MealService();
  final client = MockClient();
  const getAllUri = 'http://19asdasdasd0/apiasdasdr';
  final header = {
    'Authorization': 'Bearer token',
    'Content-Type': 'application/json'
  };

  test('getAllMeals is returning Empty List', () async {
    //GIVEN
    final List<MealModel> expected = List.empty();

    when(client.get(Uri.parse(getAllUri)))
        .thenAnswer((_) async => http.Response(expected.toString(), 200));
    final List<MealModel> actual =
        await mealService.getAllMeals('user', 'token');

    expect(actual, expected);
  });

  test('getAllMeals is returning List with one Meal', () async {
    //GIVEN
    final MealModel meal1 = MealModel(
        mealId: '1',
        mealOfUserId: 'user',
        date: 'date',
        mealDaytime: 'mealDaytime',
        products: List.empty(),
        allergens: List.empty(),
        hasSideEffect: false,
        sideEffects: List.empty());
    final List<MealModel> expected = List.empty(growable: true);
    expected.add(meal1);

    // WHEN
    when(client.get(Uri.parse(getAllUri)))
        .thenAnswer((_) async => http.Response(jsonEncode(expected), 200));
    final List<MealModel> actual =
        await mealService.getAllMeals('user', 'token');

    //THEN
    expect(actual.length, expected.length);
    assert(actual[0].mealId == expected[0].mealId);
  });

  test('getAllMeals got 404 and returns Empty List', () async {
    //GIVEN
    final List<MealModel> expected = List.empty(growable: true);

    // WHEN
    when(client.get(Uri.parse(getAllUri)))
        .thenAnswer((_) async => http.Response('bad request', 404));
    final List<MealModel> actual =
        await mealService.getAllMeals('user', 'token');

    //THEN
    expect(actual.length, expected.length);
    assert(expected.isEmpty);
  });
}

This is the not working Mock

class _Fake_InternalFinalCallback<T> extends _i1.Fake
    implements _i2._InternalFinalCallback<T> {}

class _FakeRxString extends _i1.Fake implements _i3.RxString {}

class _FakeAppUserLoginDto extends _i1.Fake implements _i4.AppUserLoginDto {}

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

  @override
  bool get initialized =>
      (super.noSuchMethod(Invocation.getter(#initialized), returnValue: false)
          as bool);
  @override
  bool get isClosed =>
      (super.noSuchMethod(Invocation.getter(#isClosed), returnValue: false)
          as bool);
  @override
  _i2._InternalFinalCallback<void> get onStart =>
      (super.noSuchMethod(Invocation.getter(#onStart),
              returnValue: _Fake_InternalFinalCallback<void>())
          as _i2._InternalFinalCallback<void>);
  @override
  _i2._InternalFinalCallback<void> get onDelete =>
      (super.noSuchMethod(Invocation.getter(#onDelete),
              returnValue: _Fake_InternalFinalCallback<void>())
          as _i2._InternalFinalCallback<void>);
  @override
  _i6.Future<void> loginAsUserAndSetToken(_i4.AppUserLoginDto? input) =>
      (super.noSuchMethod(Invocation.method(#loginAsUserAndSetToken, [input]),
          returnValue: Future<void>.value(),
          returnValueForMissingStub: Future.value()) as _i6.Future<void>);
  @override
  _i6.Future<void> getTokenFromLocalStorage() =>
      (super.noSuchMethod(Invocation.method(#getTokenFromLocalStorage, []),
          returnValue: Future<void>.value(),
          returnValueForMissingStub: Future.value()) as _i6.Future<void>);
  @override
  _i6.Future<void> deleteTokenFromLocalStorage() =>
      (super.noSuchMethod(Invocation.method(#deleteTokenFromLocalStorage, []),
          returnValue: Future<void>.value(),
          returnValueForMissingStub: Future.value()) as _i6.Future<void>);
  @override
  bool isTokenValid(String? jwtToken) =>
      (super.noSuchMethod(Invocation.method(#isTokenValid, [jwtToken]),
          returnValue: false) as bool);
  @override
  void onInit() => super.noSuchMethod(Invocation.method(#onInit, []),
      returnValueForMissingStub: null);
  @override
  void onReady() => super.noSuchMethod(Invocation.method(#onReady, []),
      returnValueForMissingStub: null);
  @override
  void onClose() => super.noSuchMethod(Invocation.method(#onClose, []),
      returnValueForMissingStub: null);
  @override
  void $configureLifeCycle() =>
      super.noSuchMethod(Invocation.method(#$configureLifeCycle, []),
          returnValueForMissingStub: null);
}

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

  @override
  _i3.RxString get token => (super.noSuchMethod(Invocation.getter(#token),
      returnValue: _FakeRxString()) as _i3.RxString);
  @override
  _i3.RxString get username => (super.noSuchMethod(Invocation.getter(#username),
      returnValue: _FakeRxString()) as _i3.RxString);
  @override
  _i3.RxString get password => (super.noSuchMethod(Invocation.getter(#password),
      returnValue: _FakeRxString()) as _i3.RxString);
  @override
  String get getToken =>
      (super.noSuchMethod(Invocation.getter(#getToken), returnValue: '')
          as String);
  @override
  String get getUsername =>
      (super.noSuchMethod(Invocation.getter(#getUsername), returnValue: '')
          as String);
  @override
  bool get hasListeners =>
      (super.noSuchMethod(Invocation.getter(#hasListeners), returnValue: false)
          as bool);
  @override
  int get listeners =>
      (super.noSuchMethod(Invocation.getter(#listeners), returnValue: 0)
          as int);
  @override
  bool get initialized =>
      (super.noSuchMethod(Invocation.getter(#initialized), returnValue: false)
          as bool);
  @override
  bool get isClosed =>
      (super.noSuchMethod(Invocation.getter(#isClosed), returnValue: false)
          as bool);
  @override
  _i2._InternalFinalCallback<void> get onStart =>
      (super.noSuchMethod(Invocation.getter(#onStart),
              returnValue: _Fake_InternalFinalCallback<void>())
          as _i2._InternalFinalCallback<void>);
  @override
  _i2._InternalFinalCallback<void> get onDelete =>
      (super.noSuchMethod(Invocation.getter(#onDelete),
              returnValue: _Fake_InternalFinalCallback<void>())
          as _i2._InternalFinalCallback<void>);
  @override
  void onInit() => super.noSuchMethod(Invocation.method(#onInit, []),
      returnValueForMissingStub: null);
  @override
  _i6.Future<void> loginAsUserAndSetToken() =>
      (super.noSuchMethod(Invocation.method(#loginAsUserAndSetToken, []),
          returnValue: Future<void>.value(),
          returnValueForMissingStub: Future.value()) as _i6.Future<void>);
  @override
  void loadTokenFromLocalStorage() =>
      super.noSuchMethod(Invocation.method(#loadTokenFromLocalStorage, []),
          returnValueForMissingStub: null);
  @override
  _i6.Future<void> logOut() =>
      (super.noSuchMethod(Invocation.method(#logOut, []),
          returnValue: Future<void>.value(),
          returnValueForMissingStub: Future.value()) as _i6.Future<void>);
  @override
  bool isTokenValid(String? token) =>
      (super.noSuchMethod(Invocation.method(#isTokenValid, [token]),
          returnValue: false) as bool);
  @override
  void setUserName(String? input) =>
      super.noSuchMethod(Invocation.method(#setUserName, [input]),
          returnValueForMissingStub: null);
  @override
  void setPassword(String? input) =>
      super.noSuchMethod(Invocation.method(#setPassword, [input]),
          returnValueForMissingStub: null);
  @override
  void setToken(String? input) =>
      super.noSuchMethod(Invocation.method(#setToken, [input]),
          returnValueForMissingStub: null);
  @override
  _i4.AppUserLoginDto getLoginDto() =>
      (super.noSuchMethod(Invocation.method(#getLoginDto, []),
          returnValue: _FakeAppUserLoginDto()) as _i4.AppUserLoginDto);
  @override
  void update([List<Object>? ids, bool? condition = true]) =>
      super.noSuchMethod(Invocation.method(#update, [ids, condition]),
          returnValueForMissingStub: null);
  @override
  void refresh() => super.noSuchMethod(Invocation.method(#refresh, []),
      returnValueForMissingStub: null);
  @override
  void refreshGroup(Object? id) =>
      super.noSuchMethod(Invocation.method(#refreshGroup, [id]),
          returnValueForMissingStub: null);
  @override
  void notifyChildrens() =>
      super.noSuchMethod(Invocation.method(#notifyChildrens, []),
          returnValueForMissingStub: null);
  @override
  void removeListener(_i8.VoidCallback? listener) =>
      super.noSuchMethod(Invocation.method(#removeListener, [listener]),
          returnValueForMissingStub: null);
  @override
  void removeListenerId(Object? id, _i8.VoidCallback? listener) =>
      super.noSuchMethod(Invocation.method(#removeListenerId, [id, listener]),
          returnValueForMissingStub: null);
  @override
  void dispose() => super.noSuchMethod(Invocation.method(#dispose, []),
      returnValueForMissingStub: null);
  @override
  _i9.Disposer addListener(_i9.GetStateUpdate? listener) =>
      (super.noSuchMethod(Invocation.method(#addListener, [listener]),
          returnValue: () {}) as _i9.Disposer);
  @override
  _i9.Disposer addListenerId(Object? key, _i9.GetStateUpdate? listener) =>
      (super.noSuchMethod(Invocation.method(#addListenerId, [key, listener]),
          returnValue: () {}) as _i9.Disposer);
  @override
  void disposeId(Object? id) =>
      super.noSuchMethod(Invocation.method(#disposeId, [id]),
          returnValueForMissingStub: null);
  @override
  void onReady() => super.noSuchMethod(Invocation.method(#onReady, []),
      returnValueForMissingStub: null);
  @override
  void onClose() => super.noSuchMethod(Invocation.method(#onClose, []),
      returnValueForMissingStub: null);
  @override
  void $configureLifeCycle() =>
      super.noSuchMethod(Invocation.method(#$configureLifeCycle, []),
          returnValueForMissingStub: null);
}
srawlins commented 3 years ago

Hi @MalteBoehm thanks for filing a bug. Can you explain what is not working correctly? What is the expected behavior and what is the actual behavior?

MalteBoehm commented 3 years ago

Hey there

currently this happens

C:\Projects\nasdasdasd>dart run build_runner build --delete-conflicting-outputs Could not find a file named "pubspec.yaml" in "C:\Users\asdasd\AppData\Local\Pub\Cache\hosted\pub.dartlang.org_fe_analyzer_shared-22.0.0".

srawlins commented 3 years ago

@MalteBoehm that does not look like a bug related to Mockito. I would file a bug against the dart-lang/build GitHub repo maybe.

7agustibm commented 2 years ago

I have the same problem, but you can run this command and run successfully:

flutter pub run build_runner build