主张流从流中发出的所有物品,直到在DART中取消?
是否有任何方便的方法来断言由流
发出的所有项目,直到取消?
如果我使用:
expectLater(
stream,
emitsInOrder(<String>[
'item1',
'item2',
]),
);
并且流
emits ['item1','item2','item3']
,测试不会失败。
到目前为止,我发现的唯一方法是以下内容:
var count = 0;
final expected = ['item1', 'item2', 'item3'];
stream.listen(
expectAsync1(
(final result) {
expect(result, expected[count++]);
},
count: expected.length,
),
);
但是它有点冗长,而且不容易阅读。有一种更简单/更优雅的方式吗?
Is there any convenient way to assert all the items emitted by a Stream
in order until it is canceled?
If I use:
expectLater(
stream,
emitsInOrder(<String>[
'item1',
'item2',
]),
);
and the Stream
emits ['item1', 'item2', 'item3']
, the test won't fail.
The only way I've found so far is the following:
var count = 0;
final expected = ['item1', 'item2', 'item3'];
stream.listen(
expectAsync1(
(final result) {
expect(result, expected[count++]);
},
count: expected.length,
),
);
But it is a bit verbose and not very easy to read. Is there a simpler/more elegant way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
emitsdone
emitsdone 如果<<代码>流在某个时候关闭。例如:
该测试失败了错误:
正如@IRN所建议的,在某个时候完成的
流
s的更紧凑的替代方法是使用tolist
:如果
streamstream < /code>永远不会关闭,您可以添加超时并检查那个时期内发出的项目:
emitsDone
can be used if theStream
is closed at some point.E.g:
The test fails with error:
As @Irn suggest, a more compact alternative for
Stream
s that complete at some point is usingtoList
:If the
Stream
is never closed, you can add a timeout and check the items that have been emitted in that period:tolist
将项目收集到列表中您可以使用 (但是,什么都没有,您只需要等待超时)即可。
在所有事件发出之前,它不会捕获错误,
emitsinorder
方法对此更好。不过,不短。You can collect the items into a list, using
toList
, then compare it to your own expectation list:This does not handle the case where the stream doesn't close at all (but then, nothing does, you just have to wait for a timeout).
It doesn't catch errors until all events have been emitted, the
emitsInOrder
approach is better for that. Not shorter, though.