Flutter:如何在Widgets状态类中测试方法?

时间:2019-04-26 17:16:47

标签: unit-testing flutter widget

我有一个用于我名为DefaultDrawer的抽屉的小部件测试。我还想测试我在DefaultDrawerState.内制作的方法,例如,

class DefaultDrawerState extends State<DefaultDrawer> {

  @override
  Widget build(BuildContext context) {
    // ..build..
  }

  Widget _circleAvatar() {
    // a method I want to test
  }
}

_circleAvatar()查找者似乎无法在我的小部件或其状态内调用方法时,如何测试方法flutter_test

下面是我现在的测试文件,没有测试小部件中的方法:

void main() {
  testWidgets('Drawer Test', (WidgetTester tester) async {
    final key = GlobalKey<ScaffoldState>();
    await tester.pumpWidget(
        MaterialApp(home: Scaffold(key: key, drawer: DefaultDrawer())));

    // open drawer
    key.currentState.openDrawer();
    await tester.pump();

    // did drawer open?
    final drawerFinder = find.byType(Drawer);
    expect(drawerFinder, findsOneWidget);
  });
}

0 个答案:

没有答案