Flutter Dartz bloc test argument type not assignable issue

257 Views Asked by At

I am writing flutter test method using bloc_test and mockito library. I am getting below strange issue while mocking repository API call. It might be a simple fix but I am trying it since last couple of hours :(.

enter image description here

Similar code is present in other public repositories but here its not working.

when(() => mockRepository.getPosts())
              .thenAnswer((_) async => Right(postEntityList));

getPosts method structure :

  @override
  Future<Either<Failure, List<PostEntity>>> getPosts() async {
  }

Basic blocTest method code:

group('whenListen', () {
    blocTest('verify posts bloc tests',
        build: () {
          when(() => mockRepository.getPosts())
              .thenAnswer((_) async => postEntityList);
          return postsBloc;
        },
        act: (PostsBloc postBloc) {
          postBloc.getAllPostsUseCase();
        },
        expect: () => (isA<PostsInitial>()));
  });
1

There are 1 best solutions below

3
Usama Karim On

You are not using it in the right way. dartz package uses Right or Left classes to access the actual type. Simply change the postEntityList to Right(postEntityList)

when(() => mockRepository.getPosts())
              .thenAnswer((_) async => Right(postEntityList));