Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 用Mockito flatter模仿蜂巢_Flutter_Dart_Mockito_Flutter Test_Flutter Hive - Fatal编程技术网

Flutter 用Mockito flatter模仿蜂巢

Flutter 用Mockito flatter模仿蜂巢,flutter,dart,mockito,flutter-test,flutter-hive,Flutter,Dart,Mockito,Flutter Test,Flutter Hive,因此,基本上,我想检查我是否已将需要传递的内容传递到HiveInterface和框中,以便于存储内容 test.dart: group('cacheStoraygeUser', () { test( 'should call HiveInterface and Box to cache data', () async { when(mockHiveInterface.openBox(any)).thenAnswer((_) async =>

因此,基本上,我想检查我是否已将需要传递的内容传递到
HiveInterface
框中,以便于存储内容

test.dart

group('cacheStoraygeUser', () {
    test(
      'should call HiveInterface and Box to cache data',
      () async {
        when(mockHiveInterface.openBox(any)).thenAnswer((_) async => mockBox);
        when(mockBox.put(0, tStoraygeUserModel))
            .thenAnswer((_) async => tStoraygeUserModel);
        // act
        dataSourceImpl.cacheStoraygeUser(tStoraygeUserModel);
        // assert
        verify(mockHiveInterface.openBox(STORAYGE_USER_BOX));
        verify(mockBox.put(STORAYGE_USER_ENTRY, tStoraygeUserModel));
      },
    );
  });
我的
dataSourceImpl.cacheStoraygeUser()实现


提前谢谢

此问题已在Mockito 5.0.9中修复

问题源于Box实现了BoxBase而不是扩展它


旧版本的Mockito无法识别这一点,因此,putAt和getAt以及其他方法不会在模拟类中生成。

可能是因为您在第一个代码中没有
wait
ing?我稍微更改为
wait dataSourceImpl.cacheStoraygeUser(tStoraygeUserModel)并且问题仍然存在
@override
  Future<void> cacheStoraygeUser(
      StoraygeUserModel storaygeUserModelToCache) async {
    /// Precaution to ensure that [STORAYGE_USER_BOX] has been opened.
    ///
    /// If the box, is in fact not opened, Hive will just return the box since
    /// the box is a Singleton. I think.
    final box = await hiveInterface.openBox(STORAYGE_USER_BOX);
    box.put(STORAYGE_USER_ENTRY, storaygeUserModelToCache);
  }
type 'Null' is not a subtype of type 'Future<void>'
MockBox.put
package:hive/…/box/box_base.dart:80
test(
  'should return StoraygeUser from StoraygeUserBox when there is one in the cache',
  () async {
    // arrange
    when(mockHiveInterface.openBox(any)).thenAnswer((_) async => mockBox);
    when(mockBox.getAt(any)).thenAnswer((_) async => tStoraygeUserModel);
    // act
    final result = await dataSourceImpl.getCachedStoraygeUser();
    // assert
    verify(mockHiveInterface.openBox(any));
    verify(mockBox.getAt(any));
    expect(result, equals(tStoraygeUserModel));
  },
);