mardi 30 mars 2021

Testing in dart using Mockito and with new null safety rules

I may have misunderstood the null safety part, but I have a function that returns Future() and I'm just testing if it returns normally. That is where my issue begins because I have tried many ways to test if it returns void and with new null safety I can't return null and void isn't something you explicitly return. So how do you go about using the when().thenReturn() or when().thenAnswer()?

I've tried looking around here and other parts of the web but found nothing similar to my problem.

This is my test:

test(
  'should return void when a child data is being created in the database',
  () async {
    // arrange
    when(remoteDataSource.createData(
            collectionName, child0.toJson(), child0.uid!))
        .thenAnswer((_) {
      return Future<void>(() {});
    });
    // act
    final Either<Failure, void> result =
        await repository.createChildData(child0.uid!, child0);
    // assert
    verify(repository.createChildData(child0.uid!, child0));
    expect(result, equals(const Right<Failure, void>({})));
  },
);

Debug Output:

type 'Null' is not a subtype of type 'Future<void>'

package:grow_v1/features/grow/data/datasources/remote/firebase/grow_remote_datasoure.dart 73:16 MockRemoteDataSource.createData test/features/grow/data/repositories/child_repository_test.dart 89:31 main.. test/features/grow/data/repositories/child_repository_test.dart 87:7 main..

✖ Tests that should occur successfully should return void when a child data is being created in the database Exited (1)

Aucun commentaire:

Enregistrer un commentaire