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));
},
);