小编apa*_*sio的帖子

如何在 Flutter 中测试流中有延迟的 StreamBuilder?

我有一个有延迟的流。StreamBuilder 永远不会从测试中的流中获取值“foo”。如果删除延迟,则测试按预期工作。我如何告诉测试等待这个延迟?

testWidgets('stream builder', (tester) async {
  final textKey = Key('textKey');
  final stream = Observable.just('foo').delay(Duration(milliseconds: 1));

  var app = MaterialApp(
    home: StreamBuilder<String>(
      stream: stream,
      builder: (context, snapshot) => Text(
            snapshot.data ?? 'no data',
            key: textKey,
          ),
    ),
  );

  await tester.pumpWidget(app);
  await tester.pump(Duration.zero);

  Text text = tester.widget(find.byKey(textKey));
  expect(text.data, equals('foo'));
});
Run Code Online (Sandbox Code Playgroud)

这失败并出现错误

??? EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ???????????????????????????
The following TestFailure object was thrown running a test:
  Expected: 'foo'
  Actual: 'no data'
   Which: is different.
          Expected: foo
            Actual: no data …
Run Code Online (Sandbox Code Playgroud)

flutter

2
推荐指数
1
解决办法
1804
查看次数

标签 统计

flutter ×1