首页 > 解决方案 > Flutter:如何 Widget 测试 使用 Widget 测试共享包

问题描述

我在尝试测试Share包时遇到问题。我的代码如下所示:

IconButton(
  key: Key('share-button'),
  icon: Icon(Platform.isAndroid ? Icons.share : CupertinoIcons.share),
  onPressed: () {
     if (activity.shareLink != null) {
      Share.share('${activity.shareLink}');
     } else {
      ScaffoldMessenger.of(context).showSnackBar(
       SnackBar(
        content: Text('Request Error'),
       ),
      );
     }
    },
   )

我提供了 [activity.shareLink],我的测试如下所示:

await tester.tap(find.byKey(Key('share-button')));
await tester.pump(Duration(seconds: 1));
expect(find.byType(Share), findsOneWidget); 

我的例外是:

══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following TestFailure object was thrown running a test:
  Expected: exactly one matching node in the widget tree
  Actual: _WidgetTypeFinder:<zero widgets with type "Share" (ignoring offstage widgets)>
   Which: means none were found but one was expected

标签: flutterflutter-testflutter-packages

解决方案


推荐阅读