我正在使用新的库 bloc_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),
],
);
这是 ReservationsLoadSuccess 的实现
class ReservationsLoadSuccess extends ReservationState {
final List<Reservation> list;
ReservationsLoadSuccess(this.list);
@override
List<Object> get props => [list];
}
其中 ReservationState 扩展 Equatable 现在,在运行测试时,您会收到以下错误
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]
基本上说实际列表中位置 2 的状态 ReservationsLoadSuccess 不等于预期列表中的对等点。
我尝试在 ReservationsLoadSuccess 类中覆盖 == 运算符,如下所示
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);
}
但这似乎不起作用,并且运行测试仍然输出相同的错误。我让它工作的唯一方法是让 props 方法返回一个空列表或添加任何其他虚拟变量并将其传递给 props 列表。
有什么方法可以使类在列表参数方面相等吗?