用 Mockito Flutter 模拟 Hive
Mocking Hive with Mockito Flutter
所以基本上,当我想存储一些东西时,我想检查我是否已经传递了我需要传递给 HiveInterface
和 Box
的东西。
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()
的实施:
@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
我已经为 HiveInterface
和 Box
生成了模拟 类。我认为如果我想测试 Hive,我应该这样做,因为我似乎无法为 Hive 本身生成 Mock 类。但是,如果您知道更好或正确的解决方案,请告诉我。
我还编写了另一个从 Hive 获取内容的测试。这工作得很好。
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));
},
);
提前致谢!
此问题已在 Mockito 5.0.9 中修复
问题源于 Box 实现 BoxBase 而不是 扩展 它。
旧版本的 Mockito 无法选择它,因此,putAt 和 getAt 以及其他方法不会在模拟中生成 类。
所以基本上,当我想存储一些东西时,我想检查我是否已经传递了我需要传递给 HiveInterface
和 Box
的东西。
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()
的实施:
@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
我已经为 HiveInterface
和 Box
生成了模拟 类。我认为如果我想测试 Hive,我应该这样做,因为我似乎无法为 Hive 本身生成 Mock 类。但是,如果您知道更好或正确的解决方案,请告诉我。
我还编写了另一个从 Hive 获取内容的测试。这工作得很好。
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));
},
);
提前致谢!
此问题已在 Mockito 5.0.9 中修复
问题源于 Box 实现 BoxBase 而不是 扩展 它。
旧版本的 Mockito 无法选择它,因此,putAt 和 getAt 以及其他方法不会在模拟中生成 类。