简体   繁体   中英

Dartz Right containing List throws Error when comparing with expect() function

Simple code like this

expect(Right(['Two', 'Three']), equals(Right(['Two', 'Three'])));

throws error:

ERROR: Expected: Right<dynamic, List>:<Right([Two, Three])>
Actual: Right<dynamic, List>:<Right([Two, Three])>

What am I doing wrong? Both lists are identical and both has equatable elements.

Dart is checking for referential equality as default, and here you are compoaring two arrays which have equal values but not equal reference. Constant lists will behave as you expected, while runtime defined lists will not.

final a = ['Two', 'Three'] == ['Two', 'Three']; // false
final b = const ['Two', 'Three'] == const ['Two', 'Three']; // true

It can be confusing since this will pass:

expect(['Two', 'Three'], equals(['Two', 'Three']));

The testing libary has default matchers of iterators and will do a deep check to match all the strings in the list, therefore the above passes. That is not the case for the Right data type, which will fallback to the == -operator and therefore fails, since it will return false as shown above.

Dartz has a list implementation (immutable) that checks for deep equality which will succeed, or with const as described above:

final a2 = ilist('Two', 'Three') == ilist('Two', 'Three') // true

expect(Right(ilist(['Two', 'Three'])), equals(Right(ilist(['Two', 'Three']))));

expect(Right(const ['Two', 'Three']), equals(Right(const ['Two', 'Three'])));

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