简体   繁体   中英

Flutter Unit Test Fails on Subclass

Method tested:

@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());
      }
    }
  }

Trying to test the failure scenario on the localDataSource .

The class structure for the failures looks like this:

abstract class Failure {
  Exception? exception;

  Failure() : exception = null;
}

class CacheFailure extends Failure {}

Simple enough, I think. And here is my test:

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

The last line fails with this error:

Expected: Left<CacheFailure, dynamic>:<Left(Instance of 'CacheFailure')>
Actual: Left<Failure, SampleModel>:<Left(Instance of 'CacheFailure')>

I'm confused since the method clearly returns a CacheFailure but the test suggests that I am returning the super class Failure . Further, why does this matter? A CacheFailure is a Failure .

Probably a simple oversight, but I just can't see it.

That expect simply compares result == Left(CacheFailure()) in my thought.

How about using isA<Left<Failure, SampleModel>>() matcher?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM