felangel / mocktail

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

put HIVE return type 'Null' is not a subtype of type 'Future<void>' in mocktail #127

Closed Mo0Khaled closed 1 year ago

Mo0Khaled commented 2 years ago

i am trying to test cash articles with HIVE package put it throws a type 'Null' is not a subtype >of type 'Future

I stubed it but I don't know why

and the print shows that it's Future

but still shows that is null not Future

test:

  group("cache last gotten articles", () {
    test('should cache the last gotten articles', () async {
      // arrange
      final expectedJsonArticles = jsonEncode(fixture('cached_articles'));
      when(() => mockHive.openBox(articles))
          .thenAnswer((_) async => mockHiveBox);
      when(() => mockHiveBox.put(articles, expectedJsonArticles)).thenAnswer((_) async =>true);
      print(mockHiveBox.put(articles, expectedJsonArticles).runtimeType)  ;
      final x = mockHiveBox.put(articles, expectedJsonArticles);
      // act
     await  articleLocaleDataSourceImpl.cacheArticleLocale(tArticlesList);
      // assert
      verify(()=>x).called(1);
      verify(() => mockHive.openBox(articles)).called(1);

    });
  });

function:

  Future<void> cacheArticleLocale(List<ArticleEntity> articles) async {
    final box = await hive.openBox(LocaleDbKeys.articleBox);
    final Map<String, dynamic> parsedArticles = {};
    parsedArticles['articles'] =
        articles.map((article) => (article as ArticleModel).toJson()).toList();
    box.put(
        LocaleDbKeys.articleBox, jsonEncode(parsedArticles['articles']));
  }

any idea how can I solve this

Mo0Khaled commented 2 years ago

I solve it...

the problem was with the data I put on put expectedJsonArticles on the test file and the data on the production file

 mockHiveBox.put(articles, expectedJsonArticles)
 box.put(LocaleDbKeys.articleBox, jsonEncode(parsedArticles['articles']));

is not the same

but the error message tells me that I didn't stub this!

@felangel could you help us improve this error message or could you explain if it makes sense to see this error message

DioGnDev commented 2 years ago

just create stub like this when(() => mockLocalDataSource.cacheData()).thenAnswer((_) async => Future.value()); hope this can help

felangel commented 1 year ago

I solve it...

the problem was with the data I put on put expectedJsonArticles on the test file and the data on the production file

 mockHiveBox.put(articles, expectedJsonArticles)
 box.put(LocaleDbKeys.articleBox, jsonEncode(parsedArticles['articles']));

is not the same

but the error message tells me that I didn't stub this!

@felangel could you help us improve this error message or could you explain if it makes sense to see this error message

Glad you were able to solve this. It looks like this is working as expected but if you have suggestions for how to improve the error message let me know. We can try adding more info like if you have stubbed the method be sure to verify the stubbed arguments match the real arguments? Not sure if that would make it more clear.