带有包装数组的测试流



我试图测试函数emitArray是否发出Response.Success,其值为['test']
如果我发出一个List<String>,一切都会按预期进行,但一旦我将结果列表包装在Response<List<String>>中,测试就会失败。

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

这不是我真正的代码,它只是一个简单的例子,更容易理解,以尝试识别问题。

这是我要测试的类:

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]}>

这是响应类

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}';
}
}

我想知道它是否与在响应中实现==有关。成功

没错。此特定测试失败,因为您无法将Lists与==:进行比较

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使用matcher包中的equals()来匹配事件,而equals()可以匹配列表和映射。它首先尝试用==匹配对象,然后检查它们是否是Iterable/Set/Map(并递归地进行深度匹配(,然后报告不匹配错误。

最新更新