'Throw Exception in Flutter Test
I am following this tutorial for Clean Architecture in flutter
This is my test file
test(
'should return server failure when call to remote data is unsuccessful',
() async {
// arrange
when(mockRemoteDataSource.getConcreteNumberTrivia(any))
.thenThrow(ServerException());
// act
final result = await repository.getConcreteNumberTrivia(tNumber);
// assert
verify(mockRemoteDataSource.getConcreteNumberTrivia(tNumber));
verifyZeroInteractions(mockLocalDataSource); // <--- this line is failing
expect(result, equals(Left(ServerFailure()))); // // <--- this line is failing
},
);
This is my code
Future<Either<Failure, NumberTrivia>> getConcreteNumberTrivia(int number) async {
networkInfo.isConnected;
try {
final remoteTrivia = await remoteDataSource.getConcreteNumberTrivia(number);
localDataSource.cacheNumberTrivia(remoteTrivia);
return Right(remoteTrivia);
} on ServerException {
return Left(ServerFailure());
}
}
I dont know why but coz of these 2 lines, test case is failing.
verifyZeroInteractions(mockLocalDataSource);
expect(result, equals(Left(ServerFailure())));
I have mentioned in test case to throw a server exception using when
and thenThrow
but still it goes to this line localDataSource.cacheNumberTrivia(remoteTrivia);
. I think this is the cause of the error but I am not quite sure.
I did read the docs but I could not find why is this problem occuring. What am i doing wrong? I am new to TDD in flutter. Thanks in advance :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|