【问题标题】:Locate a Radio widget in a flutter unit test在颤振单元测试中找到 Radio 小部件
【发布时间】:2020-05-13 07:32:24
【问题描述】:

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

来自debugDumpApp() 我要查找的小部件位于此 sn-p 的底部:

│      ├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');
      });

【问题讨论】:

    标签: flutter flutter-test


    【解决方案1】:

    用这个替换你的取景器:

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

    【讨论】:

    • 不起作用,因为 Radio() 构造函数需要参数 onChangedvaluegroupValue
    【解决方案2】:
     final radioFinder = find.byWidgetPredicate(
              (widget) => widget is Radio<int>,
            );
    

    【讨论】:

    • 这很好用,你可以像这样使用它:expect(radioFinder, findsNWidgets(2));
    猜你喜欢
    • 2020-12-04
    • 2023-01-12
    • 1970-01-01
    • 2020-08-18
    • 2021-12-18
    • 1970-01-01
    • 2023-03-27
    • 2021-02-22
    • 1970-01-01
    相关资源
    最近更新 更多