如何在Flutter中测试在didUpdateWidget()中实例化的小部件?

时间:2019-02-09 03:29:05

标签: asynchronous testing flutter widget

我有一个StatefulWidget,它在AnimatedCrossFade中创建一个didUpdateWidget()小部件,并将其另存为animation。这是一个简化的示例:

class BackgroundImage extends StatefulWidget {
  final Color colorA;
  final Color colorB;

  const BackgroundImage({
      this.colorA,
      this.colorB,
  });
}

class _BackgroundImageState extends State<BackgroundImage> {
  Widget _animation;

  @override
  void didUpdateWidget(Widget old) {
    super.didUpdateWidget(old);
    _buildBackgroundA(colorA).then((backgroundA) {
      _buildBackgroundB(colorB).then(backgroundB) {
        print(backgroundA); // this is not null
        print(backgroundB); // this is not null
        _animation = AnimatedCrossFade(
          duration: Duration(seconds: 15),
          firstChild: backgroundA,
          secondChild: backgroundB,
          crossFadeState: someVarToSwitchColors,
              ? CrossFadeState.showFirst
              : CrossFadeState.showSecond,
          );
       }
     }
  }

  @override
  Widget build(BuildContext context) {
    return _animation != null ? _animation : Container();
  }
}

_buildBackgroundA()_buildBackgroundB()是异步函数,它们会重新返回Future<Widget>。这在我的应用中可以正常工作-调用didUpdateWidget(),我的AnimatedCrossFade出现并在两个背景之间设置动画。

但是,我在测试中无法找到AnimatedCrossFade。我可以找到其他无状态小部件,也可以找到BackgroundImage小部件。我有类似的东西:

    await tester.pump();
    await tester.pumpAndSettle(Duration(minutes: 1));
    expect(
        find.descendant(
            of: find.byType(BackgroundImage),
            matching: find.byType(AnimatedCrossFade)),
        findsOneWidget);

此操作失败,因为找不到AnimatedCrossFade

如果我将build()函数更改为:

  @override
  Widget build(BuildContext context) {
    return AnimatedCrossFade(...);
  }

我可以找到我的小部件。因此,我怀疑这与我的expect函数完成之前执行的测试_buildBackground()有关。我尝试将pumppumpAndSettle中的持续时间更改为无效。如何强制测试等待更多时间?还有其他我想念的东西吗?

测试日志如下(带有我的照片):

Running Flutter tests.
00:00 +0: ...d/work/.../cl00:00 +0: (setUpAll)                                                                                                          00:00 +0: Test background                                              
init state called
_buildBackgroundA called
init state called
_buildBackgroundB called
...
00:00 +0 -1: Test background
    Expected: exactly one matching node in the widget tree
    Actual: ?:<zero widgets with type "AnimatedCrossFade" that has ancestor(s) with type "BackgroundImage" (ignoring offstage widgets)>
     Which: means none were found but one was expected
  This was caught by the test expectation on the following line:
 ...line 179

about to return from calling _buildBackgroundA
image: Image(image: MemoryImage(_Uint8ArrayView#af55b, scale: 1.0), width: 50.0, height: 200.0, fit: cover, alignment: center, this.excludeFromSemantics: false, filterQuality: low)
about to return from calling _buildBackgroundB
image: Image(image: MemoryImage(_Uint8ArrayView#79291, scale: 1.0), width: 50.0, height: 830.0, fit: cover, alignment: center, this.excludeFromSemantics: false, filterQuality: low)
...

0 个答案:

没有答案