gpt4 book ai didi

testing - 使用具有类型列表参数的 Dart 类,如何使其相等

转载 作者:行者123 更新时间:2023-11-28 21:35:17 26 4
gpt4 key购买 nike

我正在使用用于 flutter 的新库 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 列表。

有什么方法可以使类在列表参数方面相等?

最佳答案

您是否尝试查看包裹 equatable .在应用 BLoC 模式时经常使用它。

关于testing - 使用具有类型列表参数的 Dart 类,如何使其相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59469428/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com