主张流从流中发出的所有物品,直到在DART中取消?

发布于 2025-02-13 02:34:26 字数 622 浏览 1 评论 0原文

是否有任何方便的方法来断言由发出的所有项目,直到取消?

如果我使用:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ㄟ。诗瑗 2025-02-20 02:34:26

emitsdone emitsdone 如果<<代码>流在某个时候关闭。

例如:

test('Test', () async {
  final controller = StreamController<String>();
  final stream = controller.stream;
  final matcher = expectLater(
    stream,
    emitsInOrder(<dynamic>[
      'Item1',
      'Item2',
      emitsDone,
    ]),
  );
  controller
    ..add('Item1')
    ..add('Item2')
    ..add('Item3')
    ..close();
  await matcher;
  await controller.close();
});

该测试失败了错误:

Expected: should do the following in order:
          • emit an event that 'Item1'
          • emit an event that 'Item2'
          • be done
  Actual: <Instance of '_ControllerStream<String>'>
   Which: emitted • Item1
                  • Item2
                  • Item3
                  x Stream closed.
            which didn't be done

正如@IRN所建议的,在某个时候完成的 s的更紧凑的替代方法是使用tolist

test('Test', () async {
  final controller = StreamController<String>();
  final stream = controller.stream;
  final matcher = expectLater(stream.toList(), completion(<String>['Item1', 'Item2']));
  controller
    ..add('Item1')
    ..add('Item2')
    ..add('Item3')
    ..close();
  await matcher;
  await controller.close();
});

如果streamstream < /code>永远不会关闭,您可以添加超时并检查那个时期内发出的项目:

test('Test3', () async {
  final controller = StreamController<String>();
  final stream = controller.stream.timeout(const Duration(milliseconds: 200));
  final matcher = expectLater(
    stream,
    emitsInOrder(<dynamic>[
      'Item1',
      'Item2',
    ]),
  );
  controller
    ..add('Item1')
    ..add('Item2');
  await matcher;
  await controller.close();
});

emitsDone can be used if the Stream is closed at some point.

E.g:

test('Test', () async {
  final controller = StreamController<String>();
  final stream = controller.stream;
  final matcher = expectLater(
    stream,
    emitsInOrder(<dynamic>[
      'Item1',
      'Item2',
      emitsDone,
    ]),
  );
  controller
    ..add('Item1')
    ..add('Item2')
    ..add('Item3')
    ..close();
  await matcher;
  await controller.close();
});

The test fails with error:

Expected: should do the following in order:
          • emit an event that 'Item1'
          • emit an event that 'Item2'
          • be done
  Actual: <Instance of '_ControllerStream<String>'>
   Which: emitted • Item1
                  • Item2
                  • Item3
                  x Stream closed.
            which didn't be done

As @Irn suggest, a more compact alternative for Streams that complete at some point is using toList:

test('Test', () async {
  final controller = StreamController<String>();
  final stream = controller.stream;
  final matcher = expectLater(stream.toList(), completion(<String>['Item1', 'Item2']));
  controller
    ..add('Item1')
    ..add('Item2')
    ..add('Item3')
    ..close();
  await matcher;
  await controller.close();
});

If the Stream is never closed, you can add a timeout and check the items that have been emitted in that period:

test('Test3', () async {
  final controller = StreamController<String>();
  final stream = controller.stream.timeout(const Duration(milliseconds: 200));
  final matcher = expectLater(
    stream,
    emitsInOrder(<dynamic>[
      'Item1',
      'Item2',
    ]),
  );
  controller
    ..add('Item1')
    ..add('Item2');
  await matcher;
  await controller.close();
});
高速公鹿 2025-02-20 02:34:26

tolist将项目收集到列表中

await expectLater(stream.toList(), completion(expected));

您可以使用 (但是,什么都没有,您只需要等待超时)即可。

在所有事件发出之前,它不会捕获错误,emitsinorder方法对此更好。不过,不短。

You can collect the items into a list, using toList, then compare it to your own expectation list:

await expectLater(stream.toList(), completion(expected));

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文