gpt4 book ai didi

dart - 使用包装好的数组测试流

转载 作者:行者123 更新时间:2023-12-03 03:36:58 27 4
gpt4 key购买 nike

我正在尝试测试emitArray函数发出的Response.Success,其值为['test']
如果我发出List<String>,则一切正常,但是一旦将结果列表包装在Response<List<String>>中,测试将失败。

发出结果,但与预期结果比较时失败。
我想知道它是否与==Response.Success的实现有关,我在使用IDE提供的默认实现。

这不是我拥有的真实代码,它只是一个简单的示例,在尝试识别问题时更容易理解。

这是我要测试的类(class):

class ListResponse {
final _array = BehaviorSubject<Response<List<String>>>();

Stream<Response<List<String>>> get array => _array.stream;

Future<void> emitArray() async {
_array.add(Response.success(['test']));
}

void dispose() {
_array.close();
}
}


这是我的测试:
void main() {
ListResponse underTest;
setUp(() {
underTest = ListResponse();
});

test('It should emit array', () {
final array = Response.success(['test']);

expect(
underTest.array,
emitsInOrder([
array,
emitsDone,
]),
);

underTest.emitArray();

underTest.dispose();
});
}


这是它引发的错误:
Expected: should do the following in order:
• emit an event that SuccessResponse<List<String>>:<SuccessResponse{value: [test]}>
• be done
Actual: <Instance of 'BehaviorSubject<Response<List<String>>>'>
Which: emitted • SuccessResponse{value: [test]}
x Stream closed.
which didn't emit an event that SuccessResponse<List<String>>:<SuccessResponse{value: [test]}>

这是Response类
class Response<T> {
Response._();

factory Response.success(T value) = SuccessResponse<T>;
factory Response.error(Exception error) = ErrorResponse<T>;
}

class ErrorResponse<T> extends Response<T> {
ErrorResponse(this.error): super._();

final Exception error;

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is ErrorResponse &&
runtimeType == other.runtimeType &&
error == other.error;

@override
int get hashCode => error.hashCode;

@override
String toString() {
return 'ErrorResponse{error: $error}';
}
}

class SuccessResponse<T> extends Response<T> {
SuccessResponse(this.value): super._();

final T value;

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is SuccessResponse &&
runtimeType == other.runtimeType &&
value == other.value;

@override
int get hashCode => value.hashCode;

@override
String toString() {
return 'SuccessResponse{value: $value}';
}
}

最佳答案

I'm wondering if it's related to the implementation of == in Response.Success



究竟。此特定测试失败,因为您无法将列表与 ==进行比较:

abstract class List<E> implements EfficientLengthIterable<E> {
...
/**
* Whether this list is equal to [other].
*
* Lists are, by default, only equal to themselves.
* Even if [other] is also a list, the equality comparison
* does not compare the elements of the two lists.
*/
bool operator ==(Object other);
}

解决方法是,您可以更改实现以比较对象的字符串表示形式:

  @override
bool operator ==(Object other) =>
identical(this, other) ||
other is SuccessResponse &&
runtimeType == other.runtimeType &&
value.toString() == other.value.toString();


有趣的是,传递未包装的 List<String>的对象通过了测试。发生这种情况是因为 StreamMatcher使用 equals()包中的 matcher来匹配事件,以及 equals() can match lists and maps。它首先尝试使用 ==匹配对象,然后检查它们是否为Iterable / Set / Map(并与它们进行递归深度匹配),然后报告不匹配错误。

关于dart - 使用包装好的数组测试流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60302581/

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