检测方法:
@override
Future<Either<Failure, SampleModel>> getSampleModel(String activityType) async {
if (await networkInfo.isConnected()) {
final remoteModel = await remoteDataSource.getSampleModel(activityType);
localDataSource.cacheSampleModel(remoteModel);
return Right(remoteModel);
} else {
try {
final localModel = await localDataSource.getSampleModel(activityType);
return Right(localModel);
} on CacheException {
return Left(CacheFailure());
}
}
}
字符串
尝试在localDataSource
上测试故障情况。
失败的类结构如下所示:
abstract class Failure {
Exception? exception;
Failure() : exception = null;
}
class CacheFailure extends Failure {}
型
很简单,我想。下面是我的测试:
test(
'should return failure when the call to remote data source is unsuccessful',
() async {
// arrange
when(mockNetworkInfo.isConnected()).thenAnswer((_) async => false);
when(mockLocalDataSource.getSampleModel(any)).thenThrow(CacheException());
// act
final result = await repository.getSampleModel(activityType);
// assert
verifyZeroInteractions(mockRemoteDataSource);
verify(mockLocalDataSource.getSampleModel(activityType));
expect(result, Left(CacheFailure()));
});
型
最后一行失败,并出现以下错误:
预期:Left<CacheFailure,dynamic>:<Left('CacheFailure'的示例)>
Actual:Left<Failure,SampleModel>:<Left(Instance of 'CacheFailure')>
我很困惑,因为该方法显然返回了CacheFailure
,但测试表明我返回的是超类Failure
。此外,为什么这很重要?CacheFailure
是Failure
。
可能是个小疏忽,但我就是看不出来。
2条答案
按热度按时间2vuwiymt1#
那个
expect
只是在我的思想中比较result == Left(CacheFailure())
。如何使用
isA<Left<Failure, SampleModel>>()
匹配器?vddsk6oq2#
以下内容适用于仍在搜索解决方案的用户:
字符串