Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
blaueeiner opened this issue Mar 9, 2020 · 3 comments

Comments

@blaueeiner
Copy link

blaueeiner commented Mar 9, 2020

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
Copy link

mateusfccp commented Mar 9, 2020

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
Copy link
Author

blaueeiner commented Mar 9, 2020

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
Copy link
Owner

spebbe commented Mar 24, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants