spebbe / dartz

Functional programming in Dart
MIT License
756 stars 59 forks source link

Equal if using equals() in expexted() but nevertheless get a test fail/error #39

Closed blaueeiner closed 4 years ago

blaueeiner commented 4 years ago

Hi Dartz Community, I got another issue when using Dartz connected with flutter_test package.

This is my code:

test(
        'should return remote data when the call to remote data source is successful',
        () async {
          when(mockRemoteDataSource.getActivities())
              .thenAnswer((_) async => [tActivityModel]);

          final result = await repository.getActivities();

          verify(mockRemoteDataSource.getActivities());
          expect(result, equals(Right<Failure, List<Activity>>([tActivity]))); //PROBLEM
        },
      );

This is the error: Expected: Right<Failure, List>:<Right([ActivityModel])> Actual: Right<Failure, List>:<Right([ActivityModel])>

For me somehow this does not make any sense. Hopefully you can help me :)

mateusfccp commented 4 years ago

Either will use == for equality. However, [1, 2, 3] != [1, 2, 3], because of the way Dart compares Lists.

For reference: https://stackoverflow.com/questions/10404516/how-can-i-compare-lists-for-equality-in-dart

If you are using Flutter, use listEquals


Edit:

You may also use IList instead. It correctly implements the == operator to make an item per item comparison. Plus, it has the functional methods from dartz, yay! Be warned, though, that this is a little less performatic.

blaueeiner commented 4 years ago

Thank you @mateusfccp that is true. So what I have to do in my context is to write a custom matcher if there is no other way.

I found the CustomMatcher class in the docs. The 'actual' is implemented that is clear but where to get expected from to then return a boolean after comparing both with listEquals()?

PS: I think IList is not a good solution in my context but still a good point.

spebbe commented 4 years ago

This is as solved as it will be, right? Closing for now.