在颤动单元测试中找到一个Radio小部件

时间:2020-01-27 21:45:18

标签: flutter flutter-test

有人对如何在单元测试中找到我的小部件树中的以下单选按钮提出建议吗?

我要查找的debugDumpApp()小部件位于此代码段的底部:

│      ├InkWell(gestures: [tap], clipped to BoxShape.rectangle, state: _InkResponseState<InkResponse>#e573c)
                              │      │└Actions(dispatcher: null, actions: {[<SelectAction>]: Closure: () => Action from Function '_createAction@613059085':., [<ActivateAction>]: Closure: () => Action from Function '_createAction@613059085':.})
                              │      │ └Focus(dependencies: [_FocusMarker], state: _FocusState#e7131)
                              │      │  └_FocusMarker
                              │      │   └Semantics(container: false, properties: SemanticsProperties, label: null, value: null, hint: null, hintOverrides: null, renderObject: RenderSemanticsAnnotations#e56d2 relayoutBoundary=up3)
                              │      │    └MouseRegion(listeners: [enter, exit], renderObject: RenderMouseRegion#61069 relayoutBoundary=up4)
                              │      │     └GestureDetector(startBehavior: start)
                              │      │      └RawGestureDetector(state: RawGestureDetectorState#51044(gestures: [tap], behavior: opaque))
                              │      │       └_GestureSemantics(renderObject: RenderSemanticsGestureHandler#8ee96 relayoutBoundary=up5)
                              │      │        └Listener
                              │      │         └_PointerListener(listeners: [down], behavior: opaque, renderObject: RenderPointerListener#125af relayoutBoundary=up6)
                              │      │          └Row(direction: horizontal, mainAxisAlignment: start, crossAxisAlignment: center, dependencies: [Directionality], renderObject: RenderFlex#2d677 relayoutBoundary=up7)
                              │      │           ├Radio<String>(dependencies: [_LocalizationsScope-[GlobalKey#eab41], _InheritedTheme], state: _RadioState<String>#e39e8(tickers: tracking 4 tickers))

找不到的单元测试:

    final widget = MaterialApp(
      home: Scaffold(
        body: RadioListWidget(
          radioList: radiolist,
          onChange: (value) {
            selected = value;
          },
        ),
      ),
    );

      testWidgets('it behaves the same as tapping the text', (tester) async {
        await tester.pumpWidget(widget);
        debugDumpApp();
        await tester.tap(find.byType(Radio).first);
        expect(selected, 'radio 1');
      });

2 个答案:

答案 0 :(得分:1)

 final radioFinder = find.byWidgetPredicate(
          (widget) => widget is Radio<int>,
        );

答案 1 :(得分:0)

为此替换您的查找器:

find.byType(Radio<String>().runtimeType)