I'm playing around with the new library bloc_test for flutter and I implemented the following test
blocTest('should return ReservationsLoadSucess when the use case returns a list of reservationsList',
build: () {
when(mockGetReservations(any)).thenAnswer((_) async => Right(reservationsList));
return ReservationBloc(getReservations: mockGetReservations);
},
act: (bloc) async {
bloc.add(ReservationsRequested(user));
},
expect: [
ReservationsInitial(),
ReservationsLoadInProgress(),
ReservationsLoadSuccess(reservationsList),
],
);
This is the implementation of ReservationsLoadSuccess
class ReservationsLoadSuccess extends ReservationState {
final List<Reservation> list;
ReservationsLoadSuccess(this.list);
@override
List<Object> get props => [list];
}
Where ReservationState extends Equatable Now, when running the test, you get the following error
should return ReservationsLoadSucess when the use case returns a list of reservationsList:
ERROR: Expected: [
ReservationsInitial:ReservationsInitial,
ReservationsLoadInProgress:ReservationsLoadInProgress,
ReservationsLoadSuccess:ReservationsLoadSuccess
]
Actual: [
ReservationsInitial:ReservationsInitial,
ReservationsLoadInProgress:ReservationsLoadInProgress,
ReservationsLoadSuccess:ReservationsLoadSuccess
]
Which: was ReservationsLoadSuccess:<ReservationsLoadSuccess> instead of ReservationsLoadSuccess:<ReservationsLoadSuccess> at location [2]
Basically saying that the state ReservationsLoadSuccess at position 2 in the actual list is not equal to its peer in the expected list.
I tried overriding the == operator in the ReservationsLoadSuccess class as follows
class ReservationsLoadSuccess extends ReservationState {
final List<Reservation> list;
ReservationsLoadSuccess(this.list);
final Function eq = const ListEquality().equals;
@override
List<Object> get props => [];
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is ReservationsLoadSuccess &&
runtimeType == other.runtimeType &&
eq(list, other.list);
}
But that didn't seem to work and running the test still outputted the same error. The only way I got it to work is to leave the props method returning an empty list or adding any other dummy variable and pass it to the props list.
Is there any way I can make the class equatable in regards to the list parameter?
Aucun commentaire:
Enregistrer un commentaire